OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How do I pause and resume multiple setTimeout in JavaScript?

  • Thread starter Thread starter isaeed
  • Start date Start date
I

isaeed

Guest
I am stuck into something which I can't seem to figure out. I am trying to pause and resume an animation using multiple setTimeout as it's clear from the code but it doesn't work for me.I took the code from: javascript: pause setTimeout(); I edited the code according to my program but it doesn't seem to work.It just starts the animation from the very first state for e.g I paused at var gT=setTimeout but it starts from var yT=setTimeout My code looks like this:

Code:
var start, remaining;

    
    function pause() {
        window.clearTimeout(yT);
        window.clearTimeout(gT);
        remaining -= Date.now() - start;
    };

    function resume() {
        start = Date.now();
        window.clearTimeout(yT);
        window.clearTimeout(gT);
        yT = window.setTimeout(function(){
            all();
            loop();
        }, remaining);
        gT = window.setTimeout(function(){
            all();
            loop();
        }, remaining);


    };

pause();
resume();

<p>I am stuck into something which I can't seem to figure out. I am trying to pause and resume an animation using multiple <code>setTimeout</code> as it's clear from the code but it doesn't work for me.I took the code from:
<a href="https://stackoverflow.com/questions/3969475/javascript-pause-settimeout">javascript: pause setTimeout();</a>
I edited the code according to my program but it doesn't seem to work.It just starts the animation from the very first state for e.g I paused at <code>var gT=setTimeout</code> but it starts from <code>var yT=setTimeout</code>
My code looks like this:</p>
<pre><code>var start, remaining;


function pause() {
window.clearTimeout(yT);
window.clearTimeout(gT);
remaining -= Date.now() - start;
};

function resume() {
start = Date.now();
window.clearTimeout(yT);
window.clearTimeout(gT);
yT = window.setTimeout(function(){
all();
loop();
}, remaining);
gT = window.setTimeout(function(){
all();
loop();
}, remaining);


};

pause();
resume();
</code></pre>
 

Latest posts

Online statistics

Members online
0
Guests online
5
Total visitors
5
Top