javascript setTimeout 在 jquery 中调用 ajax
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6978556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
setTimeout an ajax call in jquery
提问by PinoyStackOverflower
Is there a way to use setTimeout in this ajax call. This is my code:
有没有办法在这个ajax调用中使用setTimeout。这是我的代码:
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.
我想在这个 ajax 被触发之前做一些事情,这就是为什么我会使用 setTimeout 或会延迟这个动作的东西。
How would I do that?
我该怎么做?
Thanks in advance :)
提前致谢 :)
采纳答案by Marc Uberstein
回答by Tols
Haven't used jquery with setTimeout before but try
之前没有使用过带有 setTimeout 的 jquery,但请尝试
var t = window.setTimeout(function, delay);
replace function in the above code with your jquery function.
用你的 jquery 函数替换上面代码中的函数。
回答by Alexander Beletsky
In a complete function:
在一个完整的函数中:
complete: function () {
setTimeout(function () {
// your action here
}, 500);
}
回答by zergussino
@Tols is right, it works. Try something like this: setTimeout(function(){jQuery('.add-to-cart button.btn-cart').trigger('click');}, 500);
@Tols 是对的,它有效。尝试这样的事情:setTimeout(function(){jQuery('.add-to-cart button.btn-cart').trigger('click');}, 500);