开发者

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 :)


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.


In a complete function:

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


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜