简体   繁体   English

ajax()调用javascript函数

[英]ajax() call in javascript function

i have a jquery .click() function that executes an .ajax() method call 我有一个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
                } 
            })

when the .ajax() method executes succesfully it calls a javascript function 当.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();
        }
    }

as you can see i have an .ajax() method inside my javascript function, is this possible? 正如你可以看到我在我的javascript函数中有一个.ajax()方法,这可能吗?

I am creating loop that starts on the finish listener of the soundmanager object. 我正在创建从soundmanager对象的完成侦听器开始的循环。 So when I need to make the ajax call to get he next url I need. 因此,当我需要调用ajax来获取他所需的下一个URL时。 If my way isnt correct can you please tell me what is the best way to accomplish what i am trying to do. 如果我的方式不正确,请告诉我什么是完成我想要做的最好的方法。

Think it's fine but i would make a separate function for the ajax call so you dont need to duplicate the code twice. 认为很好,但是我将为ajax调用创建一个单独的函数,因此您无需重复两次代码。 Easier to maintain. 更易于维护。

$(".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