Execution order of JavaScript listener

Damo

I have a question regarding the order of execution of JavaScript (listener) methods. While I appreciate the below code is probably not best practise, is there any way to guarantee the order the below functions will fire when btn1 is changed?

  $(function() {
    $('#btn1').change(function(){
            doStuff();
    });
  });

  $(function() {
    $(#btn1, btn2).change(function(){
            doMoreStuff();
    });
  });

E.g. is it possible to assert that based on the order the JS code "appears in" (i.e listed in the actual js / html file), that (when #btn1 changes): 1. that doStuff() will execute first 2. that doStuff() will complete fully before doMoreStuff() is invoked – assuming all doStuff is doing is updating the DOM

I have a real example, where doStuff updates the DOM and doMoreStuff invokes an Ajax endpoint, using the updated DOM values - and want to be sure doStuff will always be invoked first (again based on the flaky design that it is "listed" first).

Thanks, Damien

James Donnelly

As far as I'm aware, jQuery ensures event handlers fire in the order in which they were created (first in, first out). Currently I can't find any documentation on this, but I'm sure I have read that somewhere in the past.

As long as your first change function isn't asynchronous, it should be the case that the first function will finish execution before the second starts. We can test this by adding a loop within our first change function:

$(function() {
  $('#btn1').change(function(){
    console.log("First change event triggered at " + +(new Date()));
    
    for (var i = 0; i < 100000000; i++)
      continue;
    
    console.log("First change event finished at " + +(new Date()));
  });
});

$(function() {
  $('#btn1, #btn2').change(function(){
    console.log("Second change event triggered at " + +(new Date()));
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select id="btn1"><option>1</option><option>2</option></select>
<select id="btn2"><option>1</option><option>2</option></select>

As you can see, the first finishes before the second starts.

Collected from the Internet

Please contact debug[email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Javascript Statement Order of Execution

From Dev

Order of Execution in JavaScript

From Dev

Unexpected execution order in javascript

From Dev

Order of function execution in javascript

From Dev

JavaScript: ORDER of function execution

From Dev

Javascript execution order incorrect

From Dev

Is the following Javascript Execution Order correct?

From Dev

What is the order of execution in JavaScript promises?

From Dev

Odd javascript code execution order

From Dev

Understanding of Javascript code execution order

From Dev

JavaScript - order of execution of <script> tags

From Dev

JavaScript Event Loop out of order execution

From Dev

javascript execution order when appended to dom

From Dev

Order of execution of functions bound to an event in Javascript

From Dev

Frame-busting JavaScript code - order of execution

From Dev

Why is javascript execution order on Android inconsistent

From Dev

JavaScript Recursion with promises -- order of execution error

From Dev

Is the order of execution of operations in Javascript guaranteed to be the same at all times?

From Dev

JavaScript execution order: why does this conditional execute after the code that follows it?

From Dev

What is the order of execution between python code and javascript code in html

From Dev

events execution order

From Dev

Issue in execution order with RewriteRules

From Java

Order of execution of instance variables

From Dev

Order of ON and JOIN in query execution

From Java

Decorator execution order

From Dev

Order of threads in execution

From Dev

CUDA thread execution order

From Dev

ListenableFuture callback execution order

From Dev

Order of execution in a Makefile

Related Related

HotTag

Archive