【问题标题】:How do i cancel time count? [duplicate]如何取消计时? [复制]
【发布时间】:2013-03-15 14:09:48
【问题描述】:

如果单击切换按钮,它下面的代码会计算分钟和秒。 如果单击/切换,是否可以使它取消所有内容,这意味着我可以单击另一个切换按钮。

function toggle(ths) {

    var clicked = $(ths).val();
    $("#lblType").html(clicked);
    $("#setCount").html(" minutes : " + minutes + " seconds : " + count);

       count = count + 1;
       if (count % 60 == 0) {
           minutes += 1;
           count = 0;
       }
        timer = setTimeout("toggle()", 1000);

    }




       <div ><label id="lblType"></label>
          <label id="setCount"></label>
     </div></p>
     <div id="planned"></div> 

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    只需拨打clearTimeout()

    clearTimeout(timer);
    

    【讨论】:

    • 循环内还是循环外?
    • @C_Major - 如果您试图通过点击取消它,它不能来自循环内部。
    【解决方案2】:

    你可以使用clearTimeout()

    clearTimeout(timer);
    

    https://developer.mozilla.org/en/docs/DOM/window.clearTimeout

    【讨论】:

    • 循环内还是循环外?
    • @C_Major - 外面。无需重复操作。
    【解决方案3】:

    window.clearTimeout(timer)

    查看http://www.w3schools.com/js/js_timing.asp了解更多信息。

    【讨论】:

      【解决方案4】:

      通过使用cleartimeout(); clearinterval();

      http://www.w3schools.com/jsref/met_win_cleartimeout.asp

      【讨论】:

        【解决方案5】:
        var timer=null;
        
        function toggle(ths) {
            var clicked = $(ths).val();
            $("#lblType").html(clicked);
            $("#setCount").html(" minutes : " + minutes + " seconds : " + count);
        
               count = count + 1;
               if (count % 60 == 0) {
                   minutes += 1;
                   count = 0;
               }
                timer = setTimeout("toggle()", 1000);
        
            }
        
        function quitTimer(){
          window.clearTimeout(timer);
          timer = null;
        }
        

        【讨论】:

        • 你的功能似乎不起作用
        • 函数的哪一部分?我只添加了 quitTimer() 函数和 timer 变量。切换功能是操作的
        猜你喜欢
        • 1970-01-01
        • 2019-10-21
        • 1970-01-01
        • 1970-01-01
        • 2016-02-04
        • 2014-06-15
        • 1970-01-01
        • 2018-04-21
        • 2020-10-08
        相关资源
        最近更新 更多