Home > Article > Web Front-end > How to implement asynchronous in javascript
How to implement asynchronous JavaScript: 1. Use the setTimeout method; 2. Use the setImmediate method; 3. Use the requestAnimationFrame method; 4. Monitor the new Image loading status; 5. Monitor the script loading status method.
The operating environment of this tutorial: Windows 7 system, JavaScript version 1.8.5, DELL G3 computer.
Javascript method to implement asynchronousness:
1. setTimeout: This is the simplest
setTimeout( function() { console.log(1); }); console.log(2);
2 , setImmediate: a new function added by IE10, specifically used to liberate the ui thread. IE10 and below and other browsers do not support
setImmediate(function(){ console.log(1); }); console.log(2);
3. requestAnimationFrame: a new product in the HTML5/CSS3 era, specially used for animation. Low-level browsers do not support
var asynByAniFrame = (function(){ var _window = window, frame = _window.requestAnimationFrame || _window.webkitRequestAnimationFrame || _window.mozRequestAnimationFrame || _window.oRequestAnimationFrame || _window.msRequestAnimationFrame; return function( callback ) { frame( callback ) }; })(); asynByAniFrame(function(){ console.log(1); }) console.log(2);
4. Monitoring the new Image loading status: Asynchronous implementation is achieved by loading an image in the data:image data format and listening to the loading status.
Although some browsers do not support the data:image image data format, they can still trigger their onerror state to achieve asynchronous implementation. However, for IE8 and below, for images in the data:image data format, onerror is executed synchronously
function asynByImg( callback ) { var img = new Image(); img.onload = img.onerror = img.onreadystatechange = function() { img = img.onload = img.onerror = img.onreadystatechange = null; callback(); } img.src = "data:image/png,"; } asynByImg(function(){ console.log(1); }); console.log(2);
5. Monitor the script loading status
The principle is the same as new Image. Generate a data:text/javascript script and monitor its loading status to achieve asynchronous implementation.
Although some browsers do not support scripts in the data:text/javascript format, they can still trigger their onerror and onreadystatechange events to achieve asynchronous implementation.
var asynByScript = (function() { var _document = document, _body = _document.body, _src = "data:text/javascript,", //异步队列 queue = []; return function( callback ) { var script = _document.createElement("script"); script.src = _src; //添加到队列 queue[ queue.length ] = callback; script.onload = script.onerror = script.onreadystatechange = function () { script.onload = script.onerror = script.onreadystatechange = null; _body.removeChild( script ); script = null; //执行并删除队列中的第一个 queue.shift()(); }; _body.appendChild( script ); } })(); asynByScript( function() { console.log(1); } ); console.log(2);
Related free learning recommendations: javascript(Video)
The above is the detailed content of How to implement asynchronous in javascript. For more information, please follow other related articles on the PHP Chinese website!