setTimeout(()=>{},0) 上面的这个函数肯定会在DOMContentLoaded之后触发 重新摘录MDN对DOMContentLoaded事件的解释: The DOMContentLoaded event is fired when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading. 重新摘录MDN对lLoad事件的解释: A very different event - load - should be used only to detect a fully-loaded page.
<divid="d"style="width: 100px; height: 100px; border: 1px solid black;"> </div> <buttonid="reset">reset</button> <divid="container"></div> </body> <scripttype="text/javascript"> $('#reset').bind('click',function(){ $('#container').empty(); }); var startTime; var log = function (msg,e) { var clientX="clientX "; if(msg.match('touchmove')){ clientX+=(e['targetTouches'][0].clientX+'').substr(0,3); } var div = $('<div></div>'); div.html((newDate().getTime()+'').substring(6) + ': ' + (newDate().getTime() - startTime) + ': ' + clientX+msg) $('#container').append(div);
}; var addListener= function (type,func) { var d = document.getElementById('d'); d.addEventListener(type,func) }; var touchStart = function (e) { startTime = newDate().getTime(); log('touchStart',e); }; var touchMove = function (e) { log('touchmove',e); }; var touchEnd = function (e) { log('touchEnd',e);
}; var mouseDown = function () { log('mouseDown'); }; var click = function () { log('clickend'); }; var mouseClick = function () { log('mouseClick'); }; var mouseUp = function () { log('mouseUp'); }; var mouseOver = function () { log('mouseOver'); }; addListener('mousedown', mouseDown); addListener('mouseover', mouseOver); addListener('click', mouseClick); addListener('mouseup', mouseUp); addListener('touchstart', touchStart); addListener('touchend', touchEnd); addListener('click', click); addListener('touchmove', touchMove);