setTimeout (slow, takes about 10 sec)
function async(callback) { setTimeout(callback, 0); }
img.onerror (data:uri)
function async(callback) { var img = new Image; img.addEventListener('error', callback, false); img.src = 'data:,foo'; }
script.onreadystatechange
function async(callback) { var script = document.createElement("script"); script.type = "text/javascript"; script.src = "javascript:"; script.onreadystatechange = function () { document.body.removeChild(script); callback(); } document.body.appendChild(script); }
script.onload (data:uri)
function async(callback) { var script = document.createElement('script'); script.onload = function() { document.body.removeChild(script); callback(); } script.src = 'data:text/javascript,'; document.body.appendChild(script); }
xhr.onreadystatechange (data:text/plain,foo)
function async(callback) { var xhr = new XMLHttpRequest; xhr.open('GET','data:text/plain,foo',true); xhr.onreadystatechange = function() { xhr.onreadystatechange = null; callback(); }; xhr.send(null); }
self.postMessage
function async(callback) { var n = ++async.count; window.addEventListener('message',function(e){ if (e.data == n) { window.removeEventListener('message', arguments.callee,false); callback(); } },false); window.postMessage(n, location.protocol + "//" + location.host); } async.count = 0;
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索异步编程
javascript 异步编程、javascript 异步、javascript 异步回调、javascript 异步请求、javascript异步加载,以便于您获取更多的相关知识。
时间: 2024-11-02 02:52:22