我正在使用 setTimeout 在 iO 和 mobile-safari 上使用 javascript 制作一些动画。
似乎当我同时定义两个超时时,较短的超时不会执行,直到较长的超时才执行。
setTimeout(a, 1000);
setTimeout(b, 300);
//it seems that both a and b will be called after 1000ms.
同样的事情似乎发生在 setInterval 上
这是一个已知问题吗? 如果是,是否有解决方法?
谢谢!
最佳答案
这不是它应该发生的方式......我怀疑你可能在其他地方有问题。
在一些不同的浏览器中尝试这个测试:http://jsfiddle.net/V6Ktd/
HTML:
<a href="#" onclick="timetest()">Test</a>
<div>First fired at <span id="resultA"></span></div>
<div>First fired at <span id="resultB"></span></div>
JS:
function timetest() {
setTimeout(function() {
document.getElementById("resultA").innerText = "a:" + new Date().getTime();
}, 1000);
setTimeout(function() {
document.getElementById("resultB").innerText = "b:" + new Date().getTime();
}, 30);
}
点击测试后在 Chrome 中我看到:
First fired at a:1302168904051
First fired at b:1302168903081
我在 Mobile Safari 中看到类似的结果。
关于javascript - 移动浏览器 javascript : multiple setTimeouts or setIntervals,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5578811/