<html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <script type="text/javascript"> var mytimer = null; function beginEvent(){ //window.setTimeout("alert('Hello, world')", 1000); mytimer = window.setTimeout("myfunc('123','456')", 3000); } function cancelEvent() { clearTimeout(mytimer); mytimer = -1; } function myfunc(t1,t2) { alert(t1+" ,"+t2); if (mytimer == -1) return; var d = document.getElementById("div1"); d.innerText = new Date(); clearTimeout(mytimer); mytimer = null; } </script> </head> <body> <br/> <br/> <div id="div1" style="position: absolute; top: 100px; left: 100px; 100px; height: 100px; border: 1px solid black;" onmouseover="beginEvent()" onmouseout="cancelEvent()"></div> </body> </html>
//鼠标放上去0.5秒后执行preview方法,鼠标移出就清除timeout $('body').on('mouseover','.ac_over',function() { timer = setTimeout(function(){ preview($('.ac_over').attr('data-id')); },500); }); $('body').on('mouseout','.ac_over',function() { clearTimeout(timer); });