1

I came across this simple(or what I thought so) question on a Javascript MCQ Test:

After how much time will the following code execute entirely?

setTimeOut(console.log("hi"),1000);
setTimeOut(console.log("hi"),1000);
setTimeOut(console.log("hi"),1000);
setTimeOut(console.log("hi"),1000);
setTimeOut(console.log("hi"),1000);

Options
    A) 1 second
    B) 2 seconds
    C) 4 seconds
    D) 5 seconds

I answered as option D) 5 seconds since EACH line above will take 1000 milliseconds to execute i.e a total of 5000 milliseconds = 5 seconds

But in the results, it said that the actual answer is Option A) 1 second.

I executed those five lines in my console (altogether) and the entire code executed after 1 second like the answer said.

I don't understand the logic behind the right answer, and why my reasoning was wrong.

4
  • 1
    None of these is correct. console.log("hi") is not wrapped in a function, so it will execute immediately. You also have an extra ), so this is likely not the actual code. Commented Jan 15, 2019 at 6:20
  • The question is typed wrongly. it must be as setTimeout( function(){console.log("hi")}, 1000 );. setTimeOut is wrong, we need a function around the console.log and also a last parenthesis should be removed! Then the answer is as its said, due to async behaviour of setTimeout, as it will return instantly and run the callback at specified time. Commented Jan 15, 2019 at 6:26
  • 1
    I removed the extra ). They were written by mistake. @Kobi Commented Jan 15, 2019 at 6:26
  • 1
    youtube.com/watch?v=8aGhZQkoFbQ see this to completely understand why happened. Commented Jan 15, 2019 at 6:52

3 Answers 3

3

because setTimeout works asynchronously, means all of these 5 statements will be executed simultaneously and all of these will start waiting for 1 second. and after one second all will be executed. hope it clears.

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

4 Comments

what if I want to run them synchronously? What extra code will I need to add? @Asim Khan
do you mean to say the first , second and so on all the setTimeout will start simultaneously ? Is that asynchronous?
@KeithMachado there are different approaches, you can use setTimeout inside the other timeout, OR you can use setInterval (depends on the scenario) .
@brk Yes.. that is async!
1

Each call you make it running in it's own background thread (it's own unique operation), when you call setTimeout, you're telling JavaScript that you want to execute your code after 1 second has passed.

If you wanted to make this last 5 seconds you would do something along the lines of:

setTimeout(function() {
    console.log("First task")

    setTimeout(function() { 
        console.log("Second task");
    },1000);
},1000);

This would execute the first task, once called it will execute the second task

Edit: I saw another post about doing it non-async, you want to avoid doing anything non-async in JavaScript as it will hold up the browser which is general is a bad practice and bad user experience

3 Comments

your second setTimeOut is not wrapped in a function() {}
Doesn't need to be wrapped if the task is just to display an output to the console
no, this way the 1000 msec will be ignored, the console.log will execute immediately (for your second/nested setTimeout)
0

From the MDN page:

The setTimeout() method ... sets a timer which executes a function or specified piece of code once the timer expires.

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.