JavaScript 中只能有一次超时吗?

发布于 2024-10-30 21:18:16 字数 606 浏览 2 评论 0原文

这段代码似乎不起作用...它在很长一段时间后才显示 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

月竹挽风 2024-11-06 21:18:16
var intervalId = window.setInterval(function() {
    $('#twCharCount').toggle();
}, 1000);

并停止闪烁window.clearInterval(intervalId);

var intervalId = window.setInterval(function() {
    $('#twCharCount').toggle();
}, 1000);

and to stop blinking window.clearInterval(intervalId);.

昵称有卵用 2024-11-06 21:18:16

难道只是一些语法问题:

var timer = [];
var t=0;
var step=1000;
counter.hide();
t += step;
timer[0] = setTimeout("$('#twCharCount').show()", t);
t += step;
timer[1] = setTimeout("$('#twCharCount').hide()", t);
t += step;
timer[2] = setTimeout("$('#twCharCount').show()", t);
t += step;
timer[3] = setTimeout("$('#twCharCount').hide()", t);
t += step;
timer[4] = setTimeout("$('#twCharCount').show()", t);

t =+step; 应该是 t += step;

并且您不应该一遍又一遍地重新声明 t 。

Could it just be some syntax problems:

var timer = [];
var t=0;
var step=1000;
counter.hide();
t += step;
timer[0] = setTimeout("$('#twCharCount').show()", t);
t += step;
timer[1] = setTimeout("$('#twCharCount').hide()", t);
t += step;
timer[2] = setTimeout("$('#twCharCount').show()", t);
t += step;
timer[3] = setTimeout("$('#twCharCount').hide()", t);
t += step;
timer[4] = setTimeout("$('#twCharCount').show()", t);

t =+step; should be t += step;

and you shouldn't redeclare t over and over again.

紙鸢 2024-11-06 21:18:16

该代码在很多方面都是错误的:(。
您的函数都被同时调用,因为它们的时间 (t) 相同。

如果你想增加t,你可能不应该在每次访问时声明它(仅使用var t = ...一次;之后你可以通过它的名称访问它: t = ...),您可能应该使用 += 而不是 =+
a += ba = a + b 的快捷方式,而 a =+ ba = parseInt 的快捷方式(b).
您可能想写:

var timer = [];
var t=0;
var step=1000;
counter.hide();
t += step;
timer[0] = setTimeout("$('#twCharCount').show()", t);
t += step;
timer[1] = setTimeout("$('#twCharCount').hide()", t);
t += step;
timer[2] = setTimeout("$('#twCharCount').show()", t);
t += step;
timer[3] = setTimeout("$('#twCharCount').hide()", t);
t += step;
timer[4] = setTimeout("$('#twCharCount').show()", t);

还有一件事,最好传递一个函数而不是一个字符串作为 setTimeout 函数的第一个参数:

setTimeout(function(){$('#twCharCount').show();},t);

抱歉,但我无法帮助自己,这就是为您优化的代码:

var timer = [],
step = 1000,
n = 4,
el = $('#twChartCount');
for(var i=0;i<n;i++)
    if(i%2)
        timer[i] = setTimeout(function(){el.hide();},i*step);
    else
        timer[i] = setTimeout(function(){el.show()},i*step);

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 (use var 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 to a = a + b, while a =+ b is a shortcut to a = parseInt(b).
You probably wanted to write:

var timer = [];
var t=0;
var step=1000;
counter.hide();
t += step;
timer[0] = setTimeout("$('#twCharCount').show()", t);
t += step;
timer[1] = setTimeout("$('#twCharCount').hide()", t);
t += step;
timer[2] = setTimeout("$('#twCharCount').show()", t);
t += step;
timer[3] = setTimeout("$('#twCharCount').hide()", t);
t += step;
timer[4] = setTimeout("$('#twCharCount').show()", t);

One more thing, it is better to pass a function than a string as the first parameter for the setTimeout function:

setTimeout(function(){$('#twCharCount').show();},t);

Sry, but i can't help my self, this is the optimized code for you :

var timer = [],
step = 1000,
n = 4,
el = $('#twChartCount');
for(var i=0;i<n;i++)
    if(i%2)
        timer[i] = setTimeout(function(){el.hide();},i*step);
    else
        timer[i] = setTimeout(function(){el.show()},i*step);
盛夏已如深秋| 2024-11-06 21:18:16

活动超时的数量实际上没有限制。

我没有看到你的代码有任何真正的问题。问题可能不是超时,而是您正在执行的命令。

附加说明(与您的问题无关,但值得一提):

  • 每个函数只需要为变量使用一次“var”语句。
  • 在这种情况下,我会使用timer.push()而不是timer[...],
  • 你可以使用单个setInterval()来代替

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):

  • You need the 'var' statement for a variable only once per function.
  • instead of timer[...] I'd use timer.push() in this case
  • you can use a single setInterval() instead
第七度阳光i 2024-11-06 21:18:16
var show = false;
window.setInterval(function() {
   if(show)
   {
      show = false;
      $('#twCharCount').show();
   }
   else
   {
      show = true;
      $('#twCharCount').hide();
   }
}, 1000);
var show = false;
window.setInterval(function() {
   if(show)
   {
      show = false;
      $('#twCharCount').show();
   }
   else
   {
      show = true;
      $('#twCharCount').hide();
   }
}, 1000);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文