【问题标题】:Manually set interval time for setInterval手动设置 setInterval 的间隔时间
【发布时间】:2016-02-06 06:29:13
【问题描述】:

我正在尝试创建应用程序,用户可以在其中选择他们希望计数器增加的频率。例如,他们在输入框中输入他们希望它每 5 秒递增 1,并且 setInterval 将设置为 5 秒等。这是我目前所拥有的:

<input type="text" id="timer"></input>
<input type="button" onlick="setTime()" value="test" />
<h1>0</h1>
<button class="play">Play</button>
<button class="pause">Pause</button>
<script type="text/javascript">
    function setTime() {
      var output = $('h1');
      var isPaused = true;
      var count = 0;
      timer = document.getElementById("timer").value;
      setInterval(function() {
        if(!isPaused) {
          time++;
          output.text(time);
        }
      }, timer*1000);
    }

    //with jquery
    $('.pause').on('click', function(e) {
      e.preventDefault();
      isPaused = true;
    });

    $('.play').on('click', function(e) {
      e.preventDefault();
      isPaused = false;
    });
</script>

我可以得到任何想法或帮助吗?我提前感谢它

【问题讨论】:

  • 仅供参考:将5 传递给setInterval 并不意味着五秒——该参数的单位是毫秒。

标签: javascript counter setinterval


【解决方案1】:

重构一点你的代码,你可以这样做:

  Every <input type="text" id="timer"></input>Seconds<br>
  <button class="start">Start</button>
<h1>0</h1>
<button class="play">Play</button>
<button class="pause">Pause</button>

还有 JS:

var interval = null;
var time = 0;
var output = $('h1');

function setTimer() {
  var seconds = +($("#timer").val());//Get the user input (and convert to number)

  interval = setInterval(function() {
        time++;
        output.text( time );
   }, seconds*1000);
}

//with jquery
$('.pause').on('click', function(e) {
  e.preventDefault();
  if(interval){
    clearInterval(interval);//Clear the created interval
  }
});

$('.play').on('click', function(e) {
  e.preventDefault();
  setTimer();
});

$('.start').click(function(){
  time = 0;
  setTimer();
});

在此处查看工作示例:https://output.jsbin.com/cukehijuwo/

【讨论】:

    【解决方案2】:

    每次用户调用setTime,您都在创建另一个间隔。这些将在您的应用程序的整个生命周期中继续创建。您应该在创建新超时之前删除超时:

    var timeoutID = null;
    
    function setTime() {
      ...
      clearTimeout(timeoutID);
      timeoutID = setTimeout(function() {
        ...
      }, timer * 1000);
    }
    

    供参考:https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setInterval

    【讨论】:

      【解决方案3】:

      完整的简单测试程序,希望对您有所帮助

      <script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
      <script type="text/javascript">
          function display() {
            $("h1").html(parseInt($("#inc").val()) + parseInt($("h1").html())) ;
            t = setTimeout("display()",parseInt($("#int").val())*1000)
          }
          function pauseT(){
              clearTimeout(t);
          }
          function continueT(){
              t = setTimeout("display()",parseInt($("#int").val())*1000)
          }
      
      </script>
      Increment Value<input type="text" id="inc"></input>
      Interval in Second<input type="text" id="int"></input>
      <input type="button" onclick="display()" value="Start" />
      <h1>0</h1>
      <button onclick="continueT()">Continue</button>
      <button onclick="pauseT()">Pause</button>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-01-12
        • 1970-01-01
        • 2020-06-21
        • 1970-01-01
        • 2018-06-26
        • 2018-01-29
        • 1970-01-01
        • 2016-05-28
        相关资源
        最近更新 更多