JavaScript Why setTimeout(function, 0) is executed after next operation, but not in the moment of calling?

I run this code in JavaScript:

setTimeout(function(){console.log("1");}, 0);
console.log("2");

But output is:

2
1

Why not vice versa?

Answer:1

The setTimeout() puts all the code in a queue, and then later based on the time, it executes. So you can see the list of procedures to execute as a line of queue. For the above code, you have:

  1. setTimeout
  2. console.log(2)
  3. Callback Functions, if any.
    1. console.log(1)

So, first thing that executes is, initialize the timer. Secondly, the console.log gets executed and you see 2. An interesting thing to note here is, setTimeout waits at least for 4 ms before executing its callback function.

Answer:2

When you use setTimeout() or one of its friends, its function is placed on a queue to be executed after all the current code has finished.

It should be noted that the spec says the minimum time is clamped to 4.

Answer:3

I am looking for a little help in trying to retrieve the ID from an URL using JavaScript. I have a main menu system which looks like this <section id="menu"> <ul id="main"> <li id="...

I am looking for a little help in trying to retrieve the ID from an URL using JavaScript. I have a main menu system which looks like this <section id="menu"> <ul id="main"> <li id="...

I have an element with ng-class attribute like this: <label class="item item-input item-floating-label" ng-class="{'has-errors':{{loginForm.$submitted && loginForm['...

I have an element with ng-class attribute like this: <label class="item item-input item-floating-label" ng-class="{'has-errors':{{loginForm.$submitted && loginForm['...

  1. ngclass expressionchangedafterithasbeencheckederror
  2. ngclass expression example
  3. ngclass expression angular 6
  4. ngclass expression angular 7
  5. ngclass expression angular 2
  6. ng class with expression angular 4
  7. ng class expression if
  8. angularjs ng class expression
  9. ngclass expression array
  10. ng-class expression in angularjs
  11. ng-class expression conditional
  12. ng-class expression ternary
  13. ng-class expression multiple
  14. ng-class expression not working
  15. ng-class expression if else
  16. ng-class expression concatenate
  17. ng-class expression function
  18. ng-class expression not updating
  19. ng-class expression angular
  20. ng-class expression syntax

Setting width for <li> tags not working. Width is changing based on content for <li> tag below is the html code Without using display: block/table/inline-block; <!DOCTYPE ...

Setting width for <li> tags not working. Width is changing based on content for <li> tag below is the html code Without using display: block/table/inline-block; <!DOCTYPE ...

  1. javascript setting width of element
  2. javascript setting width

<li ng-repeat="appliance in appliances | limitTo:2"> <div class="icon"> <img src="images/vector/Appliances/w-{{appliance.DashboardIcon}}.svg"> </div> <div ...

<li ng-repeat="appliance in appliances | limitTo:2"> <div class="icon"> <img src="images/vector/Appliances/w-{{appliance.DashboardIcon}}.svg"> </div> <div ...