就像我在这里解释的那样,我不能再使用window.setTimeout()和任何window经典函数,如clearInterval等...); 但是我需要将JS块代码调用为异步代码。
这就是我使用XHR请求的原因。
如何使用XHR实现智能替代window.setTimeout()的最佳方法?
// Not working :(
setTimeout(function(){
document.getElementById("messageTimer").innerHTML = "Happy New Year ! (old version)";
}, 10);
// with or without jQuery - but XHR
jQuery.ajax({
url: "/local/url/easy",
success: function(html, textStatus, jqXHR) {
// a loop ?
// timeout done ?
document.getElementById("messageTimer").innerHTML = "Happy New Year ! (working version)"
}});
我的代码测试: https://jsfiddle.net/mlefree/xzh3w2we/
谢谢
$({to:0})
create a jQuery object where element at index[0]
is the plain object{to:0}
, we can call.animate()
chained to the jQuery object which animates theto
property from0
to1
- guest271314