html5 - Setting Javascript's setInterval function on a variable amount -- possible? -


so i'm using setinterval incrementally change basic html text on page. simplicity, let's it's counter/blinker. possible allow user input adjust speed of setinterval value?

in code below, playspeed holds value playlife() executed at. it's defaulted value of 1500, have construct/form on page allows user increment/decrement speed. unfortunately, doesn't affect speed. have hunch because whatever playspeed when setinterval read, final , won't change because change playspeed. if right, there workaround this?

thanks!

var automatelife = setinterval(function(){playlife()}, playspeed); 

i'm guessing game-of-life game, settimeout isn't want. when playspeed changes, can re-make interval so:

clearinterval( automatelife ); automatelife = setinterval(function(){playlife()}, playspeed); 

it have new delay. reset current timer, if update moments away, pushed back. "fix" that, need have faster interval , own timing logic (e.g. add frame variable each time, when reaches delay, reset 0 , run logic)


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -