-1

i have a jquery .click() function that executes an .ajax() method call

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

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?

I am creating loop that starts on the finish listener of the soundmanager object. So when I need to make the ajax call to get he next url I need. If my way isnt correct can you please tell me what is the best way to accomplish what i am trying to do.

1 Answer 1

2

Think it's fine but i would make a separate function for the ajax call so you dont need to duplicate the code twice. 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
        }
    });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.