javascript - Manually set interval time for setInterval -


i'm trying create application user can select how want counter increment by. example, type input box want increment 1 every 5 seconds, , setinterval set 5 seconds, etc. here's have far:

<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 ideas or help? appreciate in advance

refactoring little code, can 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 js:

var interval = null; var time = 0; var output = $('h1');  function settimer() {   var seconds = +($("#timer").val());//get user input (and convert number)    interval = setinterval(function() {         time++;         output.text( time );    }, seconds*1000); }  //with jquery $('.pause').on('click', function(e) {   e.preventdefault();   if(interval){     clearinterval(interval);//clear created interval   } });  $('.play').on('click', function(e) {   e.preventdefault();   settimer(); });  $('.start').click(function(){   time = 0;   settimer(); }); 

see working example here: https://output.jsbin.com/cukehijuwo/


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -