2011-08-08 42 views
1

有沒有在這個Ajax調用中使用setTimeout的方法。這是我的代碼:setTimeout在jQuery中調用ajax

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'); 
} 
}); 

我想要做的事在此之前阿賈克斯將被觸發,這就是爲什麼我會使用的setTimeout或東西會推遲這一行動。

我該怎麼做?

感謝提前:)

+0

所以,如果你想使用它 - 你爲什麼不只是這樣做? ;-) – zerkms

+0

@zerkms:我如何將它放在setTimeout函數中? – PinoyStackOverflower

+0

像其他任何js代碼? – zerkms

回答

1

您可以使用beforeSendExample

$(function() { 

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

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

參考this

+0

呃,找不到'延遲'屬性。你從哪裏拿走它? – zerkms

+1

也應該是'beforeSend:callBeforeAjax',而不是'beforeSend:callBeforeAjax()' – zerkms

+0

你不需要delay屬性,因爲beforeSend會在Ajax之前被調用。我已經更新了我的答案,沒有延遲方法,但我現在將添加參考。 –

2

沒有使用jQuery的用的setTimeout之前,但儘量

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

用上述代碼替換jquery函數中的函數。

1

@Tols是對的,它的工作原理。嘗試是這樣的:setTimeout(function(){jQuery('.add-to-cart button.btn-cart').trigger('click');}, 500);

2

在一個完整的功能:

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