Mat*_*oud 7 javascript xmlhttprequest settimeout
就像我在这里解释的那样,我不能再使用window.setTimeout()和任何窗口经典函数,如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)"
}});
Run Code Online (Sandbox Code Playgroud)
我的小提琴测试:https://jsfiddle.net/mlefree/xzh3w2we/
TKS
尝试使用jQuery 版本 3.0 .animate()
,它现在使用requestAnimationFrame
// Creates a jQ object where elem set to index of [0]
// a plain object with value of 0 `{to:0}`
// call .animate() chained to the jQ object
// Animates `{to:0}` value from 0 - 1
// $({to:0}).animate({to:1}
var duration = 5000;
$({to:0}).animate({to:1}, duration, function() {
// do stuff after `duration` elapsed
$("#messageTimer").html("Happy New Year ! (working version)")
})
Run Code Online (Sandbox Code Playgroud)
<script src="https://code.jquery.com/jquery-3.0.0-beta1.min.js"></script>
<div id="messageTimer"></div>
Run Code Online (Sandbox Code Playgroud)