It's probably already clear what I'm looking for, but I will describe it again. The task is to create something like setInterval, but so that it would be possible to change the delay time. I still hoped that everything can be done simply, but no ...

window.timeset = 1000; function play() { el = $('.main'); num = parseInt(el.text());; el.text(num+1); window.timeset = Math.random(); } setInterval(play, timeset); 

I will need to change not one time, but each tick.

    2 answers 2

    Then you don't need setInterval , but the setTimeout call chain.

      function play() { var el = $('.main'); var num = parseInt(el.text()); el.text(num + 1); var timeout = Math.round(Math.random() * 1000); $("#timeout").text(timeout); setTimeout(play, timeout); } setTimeout(play, 1000); 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> Iteration = <span class="main">0</span> <br /> Timeout = <span id="timeout"></span> 

    • So you can change 10 milliseconds to another value? As I said at the very beginning - the values ​​will be infinity and they will be generated. I checked - it didn't work out for me .. - Telion
    • I need an eternal script with different gaps. I made a small edit in the post that would be clearer what I want to do. - Telion
    • setInterval(play, Math.random()); By the way, so probably just as detailed as possible. Just need to have different values ​​each time. - Telion
    • Thank you very much, exactly what you need! - Telion

    You can also use requestAnimationFrame() - the function passed there will be called at every “frame” of the screen redraw - and check the current time: it's time or not yet to “shoot” again.

    So, the time of the next operation, depending on the slider, is checked not only at the next operation, but often and often. You can instantly flip left for a frequent blink from the rightmost “long” blink to the left - and it will work right away. True, it looks like you have a slightly different task, and a variant with setTimeout() enough.

     var el = document.getElementById('flash') ,out = document.getElementById('out-freq') ,slider = document.getElementById('in-freq') ,period ,state = 1 ,start ,next ; function step(timestamp) { period = parseInt( slider.value, 10); if (!start) start = timestamp; next = start + period; if( next < timestamp) { start = timestamp; next = start + period; out.innerHTML = slider.value; el.style.backgroundColor = (state ^= 1) ? "#F90" : "#333"; } window.requestAnimationFrame(step); } window.requestAnimationFrame(step); 
     #flash {width:40px; height:40px; border-radius:20px; background-color:#CCC;} 
     <input id="in-freq" type="range" min="10" max="500" step="10"> частота: <span id="out-freq"></span> <div id="flash"></div>