Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript execution timing - ensure synchronous flow, not just order of execution
    primarykey
    data
    text
    <p>I am working with JavaScript (Jquery) trying to run some operations in order. In my real code, I have a function I run on <code>.ready()</code> to initiate some variables. As part of this flow I read in some data from <em>local storage</em> to remove elements which were removed in a previous visit. The elements which I remove have a css transition class applied. When the page first loads, I do not want these animations to occur, so I require that the <code>&lt;body&gt;</code> element has the <code>class="fully-loaded"</code> applied in my CSS for transition to work. I am trying to add the <code>fully-loaded</code> class only after I know all of the additions and everything else is loaded. I have tried setting up multiple <code>.ready()</code> statements, using queues, etc. in order to control the order or execution. </p> <p>While I think I have things running in order, the initiation function does not seem to be complete before I run the script to add the <code>fully-loaded</code> class. I am pasting a mini version of my code which exhibits the same problem. <strong>My question is:</strong> How can I ensure that anything run as part of my initiation function <code>init()</code> complete before I run the <code>addClass</code> call?</p> <h2>HTML body:</h2> <pre><code>&lt;body&gt; &lt;section id="secA"&gt; &lt;p&gt;Sec A&lt;/p&gt; &lt;/section&gt; &lt;section id="secB"&gt; &lt;p&gt;Sec B&lt;/p&gt; &lt;/section&gt; &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"&gt;&lt;/script&gt; &lt;script&gt; //$(document).ready( Tester.init() ); //$(document).ready( $("body").addClass('fully-loaded') ); //$(document).ready( Tester.init() ); $.when(Tester.init()).done(function(){ $("body").addClass('fully-loaded'); console.log("Desired Order: 4"); }); &lt;/script&gt; &lt;/body&gt; </code></pre> <h2>JavaScript in another file</h2> <pre><code>var Tester = (function() { function init() { initEvents(); } function initEvents(){ console.log("Desired Order: 1"); setTimeout( '$("#secA").remove(); console.log("Desired Order: 2");', 2000 ); console.log("Desired Order: 3"); } return { init : init }; })(); </code></pre> <h1>CSS Code</h1> <pre><code>section { width:200px; height:200px; position:absolute;} #secA { background-color:green; } #secB { background-color:red; } section:nth-of-type(1) { top: 0; left: 0; } section:nth-of-type(2) { top: 0; left: 50%; } .fully-loaded section { -webkit-transition: all 2.5s ease-in-out; -moz-transition: all 2.5s ease-in-out; transition: all 2.5s ease-in-out; } </code></pre> <p>I think in my real code that there is something that is taking a bit of time which is async like the setTimeout. <em>Desired Action: 4</em> occurs before <em>Desired Action: 2</em> How do I make <code>$("body").addClass('fully-loaded');</code> wait until everything started by <code>Tester.init()</code> is complete in a general way?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload