简体   繁体   English

手动设置setInterval的间隔时间

[英]Manually set interval time for setInterval

I'm trying to create application where the user can select how often they want the counter to increment by. 我正在尝试创建一个应用程序,用户可以在其中选择希望计数器增加的频率。 For example, they type into the input box that they want it to increment by 1 every 5 seconds, and the setInterval will be set to 5 seconds, etc. Here's what I have so far: 例如,他们在输入框中键入要使其每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>

Can I get any ideas or help? 我可以得到任何想法或帮助吗? I appreciate it in advance 我预先感谢

Each time the user calls setTime , you are creating another interval. 每次用户调用setTime ,您都在创建另一个间隔。 These will continue to be created throughout the lifecycle of your application. 这些将在您的应用程序的整个生命周期中继续创建。 You should remove timeouts before creating new ones: 您应该在创建新超时之前删除超时:

var timeoutID = null;

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

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

Refactoring a little your code, you can do it this way: 重构一些代码,您可以通过以下方式实现:

  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>

And the JS: 和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();
});

See the working example here: https://output.jsbin.com/cukehijuwo/ 请在此处查看工作示例: https : //output.jsbin.com/cukehijuwo/

Full simple tested program, hope this will help you 完整的简单测试程序,希望对您有所帮助

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM