javascript - order in triggering events and functions -


is there way perform in order?

html:

<button type="button">click!</button> 

js:

window.addeventlistener("hashchange", function() {     eventfunction(); }, false);  document.queryselector('button').addeventlistener('click', function() {     location.hash = "changed";     otherfunction(); });  function eventfunction() {     console.log('first'); }  function otherfunction() {     console.log('second'); } 

here jsfiddle: https://jsfiddle.net/texzj2uv/1/

just call otherfunction asynchronously:

window.addeventlistener("hashchange", function() {    eventfunction();  }, false);    document.queryselector('button').addeventlistener('click', function() {    location.hash = "changed";    settimeout(otherfunction, 0);  });    function eventfunction() {    console.log('first');  }    function otherfunction() {    console.log('second');  }
<button type="button">  click!  </button>

what happens when call location.hash = "changed"; browser pushes event handler on queue , continues call stack. if call otherfunction() right away, on call stack , called before on queue. calling settimeout(otherfunction, 0) pushes on queue behind event handler.

there great video explaining javascript loop, call stack , queue: philip roberts: heck event loop anyway? | jsconf eu 2014


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -