簡體   English   中英

ajax()調用javascript函數

[英]ajax() call in javascript function

我有一個jquery .click()函數執行.ajax()方法調用

$(".btn-play").live("click", function () {
            //set globalSortNumber
            globalSortNumber = $(this).parents("[sortnumber]").attr("sortnumber");//.attr("value") ; //change should be some type of input like mediaid //see if its possible to add the sortID to the handle span
//            alert(globalSortNumber);
            //set title

            //set mediaID
            var mediaID = $(this).parents("[mediaid]").attr("mediaid");
//          alert(mediaID);
            //ajax query to get link and excute code to launch music
            $.ajax({
                type:"POST",
                url:"ajax/AB3Ajax.asmx/GenerateLink",
                data:"{'mediaId':" + mediaId + ",'userId':" + "0" + "}",
                contentType:"application/json; charset=utf-8",
                dataType:"json",
                success:function (msg) {
                    if (msg.d != "") {
                        playsong(msg.d,null);
                        }
                        else {
                            soundManager.stopAll();
                            //look at what is already in place in mystudio
                        } 
                },
                error: function (err) {
                //add code later look at what is already in place in mystudio
                } 
            })

當.ajax()方法成功執行時,它調用一個javascript函數

function playsong(sortNumber,url) {
        if (soundManager.supported()) {
            globalSortNumber = sortNumber;
            var aSoundObject = soundManager.createSound({
                id: 'mySound' + sortNumber,
                url: url,//'/Music/mafiamusicpt2rickross.mp3',
                whileplaying: function () {
                    if (count == 0) {
                        if (this.position > 1000) {
                            this.pause();
                            pos = this.position;
                            count++;
                            this.resume();
                        }
                    } else if (count == 1) {
                        soundManager._writeDebug('old position: ' + pos);
                        soundManager._writeDebug('new position: ' + this.position);
                        // See that this.position is less than pos!
                        count++;
                    }
                },
                onfinish: function () {
                    //find the next song in the list
                    //var nextSongPosition=
                    this.destruct();

                    $.ajax({
                type:"POST",
                url:"ajax/AB3Ajax.asmx/GenerateLink",
                data:"{'mediaId':" + mediaId + ",'userId':" + "0" + "}",
                contentType:"application/json; charset=utf-8",
                dataType:"json",
                success:function (msg) {
                    if (msg.d != "") {
                        playsong(msg.d,null);
                        }
                        else {
                            soundManager.stopAll();
                            //look at what is already in place in mystudio
                        } 
                },
                error: function (err) {
                //add code later look at what is already in place in mystudio
                } 
            })
                    playsong(sortNumber++,url)
                }
            });
            aSoundObject.play();
        }
    }

正如你可以看到我在我的javascript函數中有一個.ajax()方法,這可能嗎?

我正在創建從soundmanager對象的完成偵聽器開始的循環。 因此,當我需要調用ajax來獲取他所需的下一個URL時。 如果我的方式不正確,請告訴我什么是完成我想要做的最好的方法。

認為很好,但是我將為ajax調用創建一個單獨的函數,因此您無需重復兩次代碼。 更易於維護。

$(".btn-play").live("click", function () {
    //set globalSortNumber
    globalSortNumber = $(this).parents("[sortnumber]").attr("sortnumber");//.attr("value"); //change should be some type of input like mediaid //see if its possible to add the sortID to the handle span
    //alert(globalSortNumber);
    //set title
    //set mediaID
    var mediaID = $(this).parents("[mediaid]").attr("mediaid");
    //alert(mediaID);
    //ajax query to get link and excute code to launch music
    getAudio(mediaID);
}

function playsong(sortNumber,url) {
    if (soundManager.supported()) {
        globalSortNumber = sortNumber;
        var aSoundObject = soundManager.createSound({
            id: 'mySound' + sortNumber,
            url: url,//'/Music/mafiamusicpt2rickross.mp3',
            whileplaying: function () {
                if (count == 0) {
                    if (this.position > 1000) {
                        this.pause();
                        pos = this.position;
                        count++;
                        this.resume();
                    }
                } else if (count == 1) {
                    soundManager._writeDebug('old position: ' + pos);
                    soundManager._writeDebug('new position: ' + this.position);
                    // See that this.position is less than pos!
                    count++;
                }
            },
            onfinish: function () {
            //find the next song in the list
            //var nextSongPosition=
                this.destruct();

                getAudio(mediaId);
                playsong(sortNumber++,url)
            }
        });
        aSoundObject.play();
    }
}

function getAudio(mediaID) {
    $.ajax({
        type:"POST",
        url:"ajax/AB3Ajax.asmx/GenerateLink",
        data:"{'mediaId':" + mediaId + ",'userId':" + "0" + "}",
        contentType:"application/json; charset=utf-8",
        dataType:"json",
        success:function (msg) {
            if (msg.d != "") {
                playsong(msg.d,null);
            }
            else {
                soundManager.stopAll();
                //look at what is already in place in mystudio
            } 
        },
        error: function (err) {
            //add code later look at what is already in place in mystudio
        }
    });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM