Skip to main content

Posts

Showing posts with the label asynchronous

HTML5 script element - async attribute - when (and how best) to use?

Can you confirm my understanding of HTML5's <script async> attribute? Any libraries referenced by other code in the page should not specify the async attribute. For example, my script references might appropriately look like: <script src="jquery..." /> <!-- async not used - ensure that this is loaded before JQuery UI and my code --> <script src="jquery.ui..." /> <!-- async not used - ensure that this is loaded before my code --> <script src="my_code1.js" async /> <!-- async used, for page load performance --> <script src="my_code2.js" async /> <!-- async used, for page load performance --> For any code in a $(document).ready(function () { } block, I can be assured that all async script have already loaded. Do I have this right? Source: Tips4all

JavaScript: asynchronous function that resumes to synchronous

I have a large script that is very synchronous. 1) it runs a function like this: var result = doSomethingSynchronous(myVar); if (result === 'something') { doSomethingElse(); } the doSomethingSynchronous function has a for loop that returns a value on a if statement: var i = 0; le = someVariable.length; for (; i < len; i++) { if (someVariable[i] === 'aString') { return true; } else { doSomethingElse(); } } Because the for loop and the processing can be intense, I've replaced the for loop with an iterative queue processing function that runs itself asynchronously using setTimeout after shifting one element from the array. Once it runs asynchronously of course it doesn't return the value and assigns it to the 'result' variable in the first snippet synchronously, and the if statement there always fails. My question is, is there a way to keep this part synchronous? Have the value be assigned to result before the script goes on to th