JavaScript 中只能有一次超时吗?
这段代码似乎不起作用...它在很长一段时间后才显示 twCharCount 元素一次。难道超时时间只能设置一个吗?有什么建议可以让这段代码变得更好吗? 感谢您的任何建议...
var timer = new Array();
var t=0;
var step=1000;
counter.hide();
var t =+ step;
timer[0] = setTimeout("$('#twCharCount').show()",t);
var t =+ step;
timer[1] = setTimeout("$('#twCharCount').hide()",t);
var t =+ step;
timer[2] = setTimeout("$('#twCharCount').show()",t);
var t =+ step;
timer[3] = setTimeout("$('#twCharCount').hide()",t);
var t =+ step;
timer[4] = setTimeout("$('#twCharCount').show()",t);
好吧..我很抱歉...我在写这篇文章时有点不太清醒... 当然,我一直在重新标记......这就是为什么所有的都同步执行......
This code seems not to work... It shows the twCharCount element only once after a long time. Could it be that there can only be one timeout set? Any suggestions making this code better?
Thanks for any advice...
var timer = new Array();
var t=0;
var step=1000;
counter.hide();
var t =+ step;
timer[0] = setTimeout("$('#twCharCount').show()",t);
var t =+ step;
timer[1] = setTimeout("$('#twCharCount').hide()",t);
var t =+ step;
timer[2] = setTimeout("$('#twCharCount').show()",t);
var t =+ step;
timer[3] = setTimeout("$('#twCharCount').hide()",t);
var t =+ step;
timer[4] = setTimeout("$('#twCharCount').show()",t);
ok .. i'm sorry ... i kind of was not verry awake while writing this ...
of course im redecalring all the time ... this is why the all execute snychronously ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
并停止闪烁
window.clearInterval(intervalId);
。and to stop blinking
window.clearInterval(intervalId);
.难道只是一些语法问题:
t =+step;
应该是t += step;
并且您不应该一遍又一遍地重新声明 t 。
Could it just be some syntax problems:
t =+step;
should bet += step;
and you shouldn't redeclare t over and over again.
该代码在很多方面都是错误的:(。
您的函数都被同时调用,因为它们的时间 (t) 相同。
如果你想增加
t
,你可能不应该在每次访问时声明它(仅使用var t = ...
一次;之后你可以通过它的名称访问它:t = ...
),您可能应该使用+=
而不是=+
:a += b
是a = a + b
的快捷方式,而a =+ b
是a = parseInt 的快捷方式(b)
.您可能想写:
还有一件事,最好传递一个函数而不是一个字符串作为 setTimeout 函数的第一个参数:
抱歉,但我无法帮助自己,这就是为您优化的代码:
The code is wrong in many ways :(.
Your functions are all being called in the same time because their time (t) is the same.
if you want to increment
t
, you should probably not declare it on every acces (usevar t = ...
only once; after that you can access it by it's name :t = ...
) and you should probably use+=
instead of=+
:a += b
is a shortcut toa = a + b
, whilea =+ b
is a shortcut toa = parseInt(b)
.You probably wanted to write:
One more thing, it is better to pass a function than a string as the first parameter for the
setTimeout
function:Sry, but i can't help my self, this is the optimized code for you :
活动超时的数量实际上没有限制。
我没有看到你的代码有任何真正的问题。问题可能不是超时,而是您正在执行的命令。
附加说明(与您的问题无关,但值得一提):
There is virtually no limit on the number of active timeouts.
I don't see any real problems with your code. Probably the problem is not the timeout but the command you're executing.
Additional notes (not related to your question, but worth to say):