Günther Huber
Günther Huber

Reputation: 21

Putting a delay in ajax script

anyone have any idea how t put a 10 second delay between posting the photo and tagging? The script makes the tags instantly after the photo is uploaded but I want a delay.

I think you use the setTimeout but I don't know where to put it

try {
    $.ajax({
        type: 'POST',
        url: 'https://graph.facebook.com/me/photos?url=http://thenewexcellence.com/wp-content/uploads/2009/10/new.jpg&method=POST&message=this is my great photo http://www.google.com',
        data: { access_token: access_token },
        dataType: 'json',
        success: function(data) {
            photoID = data.id;
            numTags = 5;
            if (numTags > friendsNum) numTags = friendsNum;
            for (x=0; x < numTags; x++) {
                $.getJSON('https://graph.facebook.com/'+photoID+'/tags?to='+friends[x]+'&x=0&y=0&method=POST&access_token=' + access_token, function () {});                                                                        
            }


        }
    });
} catch(e){

Upvotes: 1

Views: 177

Answers (2)

Matt Clark
Matt Clark

Reputation: 28589

You can use setTimeout function to call another function with your code that you want to be executed after the delay.

setTimeout(someFunction, millisecondDelay);

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388316

You are right about setTimeout

try {
    $.ajax({
        type: 'POST',
        url: 'https://graph.facebook.com/me/photos?url=http://thenewexcellence.com/wp-content/uploads/2009/10/new.jpg&method=POST&message=this is my great photo http://www.google.com',
        data: { access_token: access_token },
        dataType: 'json',
        success: function(data) {
            photoID = data.id;
            numTags = 5;
            if (numTags > friendsNum) numTags = friendsNum;

            //set the delay here
            setTimeout(function(){
                for (x=0; x < numTags; x++) {
                    $.getJSON('https://graph.facebook.com/'+photoID+'/tags?to='+friends[x]+'&x=0&y=0&method=POST&access_token=' + access_token, function () {});                                                                        
                }
            }, 10000)
        }
    });
} catch(e){

}

Upvotes: 5

Related Questions