【问题标题】:Can there only be one timeout in JavaScript?JavaScript 中只能有一次超时吗?
【发布时间】:2011-04-07 16:37:21
【问题描述】:

这段代码似乎不起作用......它在很长一段时间后只显示了一次 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);

好吧..对不起...我在写这篇文章时有点不清醒... 当然我一直都在重新标记...这就是为什么所有同步执行...

【问题讨论】:

  • 可以有多个超时。您在赋值运算符中使用了 + 和 = 错误的方式,并且在第一次声明之后也没有必要再次用 var 声明 t 。最后,使用匿名函数而不是传递一个字符串来超时,或者传递一个命名函数;您可以在命名/匿名函数之外缓存对 jQuery 包装集的引用
  • 看起来你想模仿一个完全弃用的老式 <blink> 东西?那么它是否有理由被弃用? en.wikipedia.org/wiki/Blink_element
  • 你为什么要重新创建setInterval

标签: javascript timer settimeout


【解决方案1】:
var intervalId = window.setInterval(function() {
    $('#twCharCount').toggle();
}, 1000);

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

【讨论】:

  • @Shaz,没什么,只是 $('#twCharCount').show() 在我看来很像 jQuery 代码,但我当然可能在这里错了。
  • @Shaz $('#twCharCount').show() 不是 jQuery?它不能比不起作用的 OP 解决方案更令人困惑。
【解决方案2】:

可能只是一些语法问题:

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。

【讨论】:

    【解决方案3】:

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

    如果你想增加t,你可能不应该在每次访问时都声明它(只使用一次var t = ...;之后你可以通过它的名字访问它:t = ...)并且你应该使用@987654324 @ 而不是=+
    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);
    

    【讨论】:

    • 可能什么都没有...=+ 只完成了一个简单的任务。也许他尝试了+= 并遇到了问题,因为这对初始化无效,但正如您所注意到的,解决方案是停止重新声明变量。
    【解决方案4】:

    对活动超时的数量几乎没有限制。

    我认为您的代码没有任何实际问题。问题可能不是超时,而是您正在执行的命令。

    补充说明(与您的问题无关,但值得一说):

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

    【讨论】:

      【解决方案5】:
      var show = false;
      window.setInterval(function() {
         if(show)
         {
            show = false;
            $('#twCharCount').show();
         }
         else
         {
            show = true;
            $('#twCharCount').hide();
         }
      }, 1000);
      

      【讨论】:

        猜你喜欢
        • 2017-03-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-24
        • 1970-01-01
        • 1970-01-01
        • 2017-09-10
        • 1970-01-01
        相关资源
        最近更新 更多