就像我解释的那样here http://blog.mlefree.com/2016/02/settimeout-alternative-as-happy-new.html,我不能再使用 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)"
}});
我的小提琴测试:https://jsfiddle.net/mlefree/xzh3w2we/ https://jsfiddle.net/mlefree/xzh3w2we/
Tks
尝试使用jQuery 3.0 版本 http://blog.jquery.com/ .animate() http://api.jquery.com/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)")
})
<script src="https://code.jquery.com/jquery-3.0.0-beta1.min.js"></script>
<div id="messageTimer"></div>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)