方法:1、利用setTimeout;2、利用setImmediate;3、利用requestAnimationFrame;4、通過監(jiān)聽“new Image”加載狀態(tài)來實(shí)現(xiàn);5、通過監(jiān)聽script加載狀態(tài)來實(shí)現(xiàn);6、利用Message等等。
本教程操作環(huán)境:windows7系統(tǒng)、javascript1.8.5版、Dell G3電腦。
原生javascript實(shí)現(xiàn)異步的方式:
1、setTimeout:這個(gè)是最簡單的
setTimeout( function() { console.log(1); }); console.log(2);
2、setImmediate :IE10添加的新功能,專門用于解放ui線程。IE10以下及其他瀏覽器不支持
setImmediate(function(){ console.log(1); }); console.log(2);
3、requestAnimationFrame :HTML5/CSS3時(shí)代新產(chǎn)物,專門用于動(dòng)畫。低級瀏覽器不支持
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、監(jiān)聽new Image加載狀態(tài):通過加載一個(gè)data:image數(shù)據(jù)格式的圖片,并監(jiān)聽器加載狀態(tài)實(shí)現(xiàn)異步。
盡管部分瀏覽器不支持data:image圖片數(shù)據(jù)格式,但仍然可以觸發(fā)其onerror狀態(tài)實(shí)現(xiàn)異步,但I(xiàn)E8及以下對data:image數(shù)據(jù)格式的圖片,onerror為同步執(zhí)行
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、監(jiān)聽script加載狀態(tài)
原理同new Image是一樣的,生成一個(gè)data:text/javascript的script,并監(jiān)聽其加載狀態(tài)實(shí)現(xiàn)異步。
盡管部分瀏覽器不支持data:text/javascript格式數(shù)據(jù)的script,但仍然可以觸發(fā)其onerror、onreadystatechange事件實(shí)現(xiàn)異步。
var asynByScript = (function() { var _document = document, _body = _document.body, _src = "data:text/javascript,", //異步隊(duì)列 queue = []; return function( callback ) { var script = _document.createElement("script"); script.src = _src; //添加到隊(duì)列 queue[ queue.length ] = callback; script.onload = script.onerror = script.onreadystatechange = function () { script.onload = script.onerror = script.onreadystatechange = null; _body.removeChild( script ); script = null; //執(zhí)行并刪除隊(duì)列中的第一個(gè) queue.shift()(); }; _body.appendChild( script ); } })(); asynByScript( function() { console.log(1); } ); console.log(2);
6、Message:html5新技能,通過監(jiān)聽window.onmessage事件實(shí)現(xiàn),然后postMessage發(fā)送消息,觸發(fā)onmessage事件實(shí)現(xiàn)異步
var asynByMessage = (function() { //異步隊(duì)列 var queue = []; window.addEventListener('message', function (e) { //只響應(yīng)asynByMessage的召喚 if ( e.data === 'asynByMessage' ) { e.stopPropagation(); if ( queue.length ) { //執(zhí)行并刪除隊(duì)列中的第一個(gè) queue.shift()(); } } }, true); return function( callback ) { //添加到隊(duì)列 queue[ queue.length ] = callback; window.postMessage('asynByMessage', '*'); }; })(); asynByMessage(function() { console.log(1); }); console.log(2);
7、Promise: ES6的新技能,具有異步性質(zhì)
var asynByPromise = (function() { var promise = Promise.resolve({ then : function( callback ) { callback(); } }); return function( callback ) { promise.then(function(){ callback(); }) }; })(); asynByPromise(function() { console.log(1); }); console.log(2);