最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - setTimeout an ajax call in jquery - Stack Overflow

programmeradmin1浏览0评论

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=".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);
                },
    plete: 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 :)

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./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);
                },
    plete: 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 :)

Share Improve this question asked Aug 8, 2011 at 6:36 PinoyStackOverflowerPinoyStackOverflower 5,30218 gold badges66 silver badges134 bronze badges 6
  • So if you want to use it - why didn't you just do that?! ;-) – zerkms Commented Aug 8, 2011 at 6:38
  • @zerkms: How would I put it in a setTimeout function? – PinoyStackOverflower Commented Aug 8, 2011 at 6:39
  • Can you show me an example please using my code above. Thanks :) – PinoyStackOverflower Commented Aug 8, 2011 at 6:44
  • uhm, have you ever used setTimeout? – zerkms Commented Aug 8, 2011 at 6:44
  • 1 any reason for that? "Won't work" is not an explanation of an issue. – zerkms Commented Aug 8, 2011 at 6:47
 |  Show 1 more ment

4 Answers 4

Reset to default 2

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 plete function:

plete: 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) {},
        plete: 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)

  1. 暂无评论