0

I've already seen below referenced questions before writing this post.

how-to-pause-a-settimeout-call

how-to-pause-a-settimeout-function

how-to-pause-a-function-in-javascript

delay-running-a-function-for-3-seconds

Question

I've below code on which I want after some time my setTimeout function is paused. Then re-continue.

 typingCallback(that) {
     let total_length = that.typewriter_text.length;
     let current_length = that.typewriter_display.length;
     if (that.rewind === false) 
     {
         if(total_length  == current_length) 
         {
             // Here I want to pause this function for 3 seconds
            clearTimeout(3000);
            that.rewind = true;
         }
         else
         {
             that.typewriter_display += that.typewriter_text[current_length];
             console.log('Loop#1: ' + that.typewriter_display + " " + 'Length' + current_length);
         }
      } 
      else if (that.rewind === true)
      {
          if(current_length == 0) 
          {    
              that.rewind = false;
          }
          else
          {
              that.typewriter_display = that.typewriter_display.slice(0, -1);
              console.log('Loop#2: ' + that.typewriter_display + " " + 'Length' + current_length);
          }
      }
      setTimeout(that.typingCallback, 75, that);
   }
Ahmer Ali Ahsan
  • 4,118
  • 15
  • 33
  • 64
  • 2
    Depend on your version of ECMAScript, use generators or async/await functions. First solution works with this library https://github.com/tj/co , see docs for details. Second solution is just `await delay(3000)`, where `delay` function is `function delay(count) { return new Promise(resolve => setTimeout(count)) }`. In this case your function will be paused in terms of control flow logic. – Vladislav Ihost Jul 18 '17 at 07:51
  • 1
    Alternatively, if you just want `that.rewind` to be set 3 seconds later, you can do `setTimeout(() => { that.rewind = true; }, 3000);` – Serge K. Jul 18 '17 at 07:54
  • @NathanP. Really? Is that is too easy? How can I forget this. But thanks for your prompt response. – Ahmer Ali Ahsan Jul 18 '17 at 08:00
  • Herm, it won't pause the execution, your function will go through though, I didn't saw the recursive part first :/ – Serge K. Jul 18 '17 at 08:01

2 Answers2

1

Basically a simple setTimeout should do it:

typingCallback(that) {
 let total_length = that.typewriter_text.length;
 let current_length = that.typewriter_display.length;
 if (that.rewind === false) 
 {
     if(total_length  == current_length) 
     {
         // Here I want to pause this function for 3 seconds
        setTimeout(that.typingCallback,3000,that);//simply set the 3 second timeout
        that.rewind = true;
        return;//dont forget to stop the function somewhere here. If not were having two recursive timeout chains... :/
     }
       //call directly
       setTimeout(that.typingCallback,75,that);// the regular timeout
Jonas Wilms
  • 106,571
  • 13
  • 98
  • 120
0

Try this...

function live() { 
    if (dead) {
        return;
    }
    // do something when alive
    setTimeout(live,speed);
} 
live();

Hope this will work for you... :)