0

Is it possible to make a function 'idle' for a couple of seconds while it is being executed?

I tried with

 setTimeout( function(){ 
      $("#nytLevel").hide();                        
 }  , 3000 );   

But the rest of the function would just executed.

Below the setTimeout I start a function

 function timer(){
      myVar = setTimeout( function(){ 
            console.log("SLOW");
      }  , 10000 );     
 }

but when 10 seconds have passed it'll console log "SLOW", but it should console log it 13 seconds after because I've put a setTimeout to 3 seconds.

4
  • How are you executing your code ? What is the order ? Provided code does not explain it at all.... Commented Mar 13, 2016 at 18:32
  • 2
    This sounds a lot like an XY problem. Why can't you put the timer() call inside the first timeout? Commented Mar 13, 2016 at 18:32
  • @pushalu setTimeout is a asynchronous function Commented Mar 13, 2016 at 18:36
  • Timeouts are asynchronous, they do not halt the execution of code outside the callback, so nested timeouts would be the solution Commented Mar 13, 2016 at 18:36

3 Answers 3

3

setTimeout() just schedules something to run in the future and the rest of your Javascript continues to run. It does not block further Javascript execution. This is often called an "asynchronous" operation. It runs in the background and will call a callback sometime in the future when it has completed its work. It is also referred to as "non-blocking" because it does not block the rest of your Javascript execution.

Anything you want to not run until the setTimeout() fires must be put inside the setTimeout() callback or called from there.

// define function
function timer(){
    myVar = setTimeout(function() { 
          console.log("SLOW");
    }, 10000);
}

// schedule first timer
setTimeout(function() { 
    $("#nytLevel").hide();   

    // now start second timer
    timer();
 }, 3000);  

It's worth mentioning that jQuery has a .delay() method that works with animations and other functions put in the queue and it can sometimes streamline your code. In the case above, you could do this:

$("#nytLevel").delay(3000).hide().delay(10000).queue(function(next) {
    console.log("SLOW");
    next();   // keep the queue moving in case there's something else in the queue
});

Please note that .delay(xxx) only works with jQuery methods that themselves use the queue (such as animations) or with methods you put in the queue yourself using .queue() (as I've shown above).

Sign up to request clarification or add additional context in comments.

3 Comments

My code contains alot of if statements, and timer and setTimeout are not in the same. So my timer will sometimes not be called even if setTimeout is. But if setTimeout is called, timer should wait. But I guess I'll try and do a rigid approach with some variables.
@PushALU - We can help you really well when you show us your real code (with the if statements). We can't really help you much when you don't show us the actual code.
@PushALU - Added info about jQuery .delay() and .queue().
1

setTimeout() is an asynchronous function, meaning that code will not pause until the setTimeout() time is completed. If you want code to be delayed along with the setTimeout(), you can put the other code inside of the initial setTimeout()

setTimeout( function(){ 
    $("#nytLevel").hide();
    myVar = setTimeout( function(){ 
        console.log("SLOW");
    }  , 10000 );                      
}  , 3000 );

Comments

1

I wouldn't recommend this, but you could fashion a recursive function to do what you wanted, using a flag to dictate before or after timeout. In the below example you'd call it like this runAfterTimeout() or runAfterTimeout(false)

function runAfterTimeout(run) {

    if (! run) {
       console.log('about to wait 10 seconds');
       setTimeout(function(){runAfterTimeout(true)},10000);
       return;
    }

    console.log('this section runs after 10 seconds');

    setTimeout(function(){$("#nytLevel").hide();},3000);
}

Fiddle: https://jsfiddle.net/m9n1xxra/

Bear in mind, timeouts are not 100% accurate. The engine will look for an appropriate break in execution to execute what you want, but if the engine is in the middle of something else, that will execute first.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.