gpt4 book ai didi

javascript - JS setTimeout() 替代方案

转载 作者:数据小太阳 更新时间:2023-10-29 04:52:23 28 4
gpt4 key购买 nike

就像我解释的那样here ,我不能再使用 window.setTimeout() 和任何窗口经典函数,如 clearInterval 等 ...);但我需要将 JS block 代码作为异步代码调用。

这就是我使用 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)"
}});

我的 fiddle 测试:https://jsfiddle.net/mlefree/xzh3w2we/

感谢

最佳答案

尝试使用 jQuery version 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)")
})
<script src="https://code.jquery.com/jquery-3.0.0-beta1.min.js"></script>
<div id="messageTimer"></div>

关于javascript - JS setTimeout() 替代方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35133311/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com