PinoyStackOverflower
PinoyStackOverflower

Reputation: 5302

setTimeout an ajax call in jquery

Is there a way to use setTimeout in this ajax call. This is my code:

jQuery.ajax({
    type    : "POST",
    url     : dir+"all/money/myFile.php",
    data    : "page="+data.replace(/\&/g, '^'),
    success : function(msg) {
    var info = jQuery('.product_overview #allinfo').html();
    var url = '<a href="http://www.mySite.com/openMe/letmeview.php?preview='+msg+'.html" target="_blank" class="check_link_preview" rel="'+msg+'.html">Check Preview</a>';          jQuery('.option_additional').next().find('textarea:first').text(info+url);
                },
    complete: function() {
    jQuery('.add-to-cart button.btn-cart').delay(500).trigger('click');
}
});

I want to do something before this ajax will be triggered that is why I'll use setTimeout or something that would delay this action.

How would I do that?

Thanks in advance :)

Upvotes: 1

Views: 12156

Answers (4)

Marc Uberstein
Marc Uberstein

Reputation: 12541

You can use beforeSend, Example

$(function() {

    function callBeforeAjax() {
        alert('and now do ajax');
    }

    $.ajax({
        beforeSend: callBeforeAjax,
        type: "POST",
        url: "/",
        data: "",
        success: function(msg) {},
        complete: function(msg) {
            alert(msg);
        }
    });
});

Refer this

Upvotes: 1

Alexander Beletsky
Alexander Beletsky

Reputation: 19841

In a complete function:

complete: function () {
  setTimeout(function () {
     // your action here
  }, 500);
}

Upvotes: 2

zergussino
zergussino

Reputation: 821

@Tols is right, it works. Try something like this: setTimeout(function(){jQuery('.add-to-cart button.btn-cart').trigger('click');}, 500);

Upvotes: 1

Tols
Tols

Reputation: 51

Haven't used jquery with setTimeout before but try

var t = window.setTimeout(function, delay);

replace function in the above code with your jquery function.

Upvotes: 2

Related Questions