指定时间开启、结束定时器
<html> <body> <script language=javascript> var myDate = null; var hour = null; var minute = null; var second = null; var out1 = null; // 每隔5秒钟就弹出提示信息"dhm"。 var out = setInterval(function(){ myDate = new Date(); hour = myDate.getHours(); minute = myDate.getMinutes(); second = myDate.getSeconds(); if(hour == 16 && minute == 30 && second == 00){ out1 = setInterval(function(){ console.log("huahua"); }, 5000); } if(hour == 16 && minute == 31 && second == 00){ console.log(222); clearInterval(out1); } if(hour == 16 && minute == 20 && second == 00){ console.log(111); out1 = setInterval(function(){ console.log("huahua"); }, 5000); } },1000) </script> </body> </html>
注意事项:
1.指定时间需精确到秒,否则开启多个定时器,会清理不干净;
2.实践证明,就算把setInterval赋值给out1,也不能保证定时器的唯一性,解决办法参考注意事项第一条