JavaScript JavaScript Event Queue and setTimeout(…) javascript event queue priority,javascript event queue example,javascript event

What's happening with that piece of code

var start = new Date();

setTimeout(function() {
    var end = new Date();
    console.log('Time elapsed with func1:',end - start, 'ms');
}, 500);

setTimeout(function() {
    var end = new Date();
    console.log('Time elapsed with func2:',end - start, 'ms');
}, 250);

while (new Date() - start < 1000) {}

Logs:

Time elapsed with func2: 1001 ms
Time elapsed with func1: 1017 ms

I was expected that func1 will be fire first because it's the first event to be added into the event queue. Then, due to the single thread nature of JS, wait until func1 returns and then executes the next event in the queue which is func2.
So, what's happening?

Answer:1

Nothing is added to the event queue UNTIL the timer reaches its appointed time.

So, the sequence of events is as follows:

  1. You register the func1 timer for 500ms.
  2. You register the func2 timer for 250ms.
  3. You start the while spin loop.
  4. After 250ms, func2 timer fires (internal to the JS engine) and its callback is added to the event queue.
  5. After 500ms, func1 timer fires (internal to the JS engine) and its callback is added to the event queue.
  6. Your while spin loop finally finishes and the current thread of JS finishes.
  7. The JS engine pulls the next item off the event queue which is the func2 callback and executes it.
  8. When that callback is done executing, it pulls the next item off the event queue which is the func1 callback and executes it.

And thus, you get the output you see with the func2 callback executing assoon as the while spin loop is done, then the func1 callback executing.

Answer:2

No, wait. It won't add your timeout call to the event queue. It's handled by the webapi by the browser and then when your timeout kicks-off then it'll add your function to the event queue.

Whatch this: https://www.youtube.com/watch?v=8aGhZQkoFbQ#t=13m (From 13:00)

Answer:3

I trying do a small app for getting information about hardware in our small office. It is probably very simple but today isn't my best day. So I have something like this: <form> <input type=...

I trying do a small app for getting information about hardware in our small office. It is probably very simple but today isn't my best day. So I have something like this: <form> <input type=...

  1. data from html to excel
  2. data from html to php
  3. data from html to python
  4. data from html to javascript
  5. data-from html
  6. data from html form to excel
  7. data from html to js
  8. data from html table
  9. data from html tag
  10. import data from html to excel
  11. store data from html form to database
  12. extract data from html
  13. get data from html
  14. pass data from html to javascript
  15. extract data from html table
  16. get data from html form
  17. get data from html table
  18. send data from html to python
  19. send data from html to php
  20. extract data from html python

I have the following object: var message = { default: 'Default', GCM: { data: { title: titleMessage, text: textMessage } } }; When I realize: ...

I have the following object: var message = { default: 'Default', GCM: { data: { title: titleMessage, text: textMessage } } }; When I realize: ...

  1. json stringify works
  2. json.stringify not working
  3. json.stringify not working on array
  4. json.stringify not working in ie11
  5. json.stringify not working for large data
  6. json.stringify not working in ie
  7. json.stringify not working on object
  8. json.stringify not working in angular 6
  9. json.stringify not working in angular 7
  10. json.stringify not working node.js
  11. json stringify not working on nested object
  12. json.stringify not working in javascript
  13. json stringify doesn't work
  14. json stringify space not working
  15. json.stringify(data) not working

Why is this code not working? So far I have only got it to work as an input. I need it to work as a label or span. I need the label to count up 1 number on every click. <html> <head> &...

Why is this code not working? So far I have only got it to work as an input. I need it to work as a label or span. I need the label to count up 1 number on every click. <html> <head> &...

  1. label counting machine
  2. label counting machine price
  3. label counting sensor
  4. label counting machine supplier
  5. label counting rewinding machine
  6. label counting scale
  7. label counting system
  8. label counting machine manufacturers
  9. label counting machine suppliers in mumbai
  10. counting label swift
  11. counting label ios
  12. roll label counting machine
  13. automatic label counting machine
  14. rewind label counting inspection table
  15. single label counting machine
  16. spectral counting label-free proteomics
  17. carb counting label reading
  18. label free spectral counting
  19. label free cell counting
  20. carbohydrate counting label reading

I am attempting to add a class to the select element depending on what option is selected. HTML <select class="wordpress"> <option value="1" class="wordpress">Hello</option> &...

I am attempting to add a class to the select element depending on what option is selected. HTML <select class="wordpress"> <option value="1" class="wordpress">Hello</option> &...

  1. select change click
  2. change select value on click
  3. change select option on click
  4. change select option on button click
  5. change select on click jquery
  6. jquery chrome select change/click not working