js 多setInterval冲突的解决方法

来源:互联网 发布:不亦说乎的说的读音 编辑:程序博客网 时间:2024/06/05 03:23
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  <html xmlns="http://www.w3.org/1999/xhtml">  <head>  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  <title>测试</title>  </head>  <script>var firstInterval;var secondInterval;function start(){firstAlert();secondAlert();}function firstAlert(){if(firstInterval) {clearInterval(firstInterval);}fun1();firstInterval = setInterval('firstAlert()', 10000);}function secondAlert(){if(secondInterval) {clearInterval(secondInterval);}fun2();secondInterval = setInterval('secondAlert()', 1000);}    function fun1(){  console.log(getNowFormatDate() + '------ fun1');    }function fun2(){  console.log(getNowFormatDate() + '------ fun2');    }function getNowFormatDate() {var date = new Date();var seperator1 = "-";var seperator2 = ":";var month = date.getMonth() + 1;var strDate = date.getDate();if (month >= 1 && month <= 9) {month = "0" + month;}if (strDate >= 0 && strDate <= 9) {strDate = "0" + strDate;}var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate+ " " + date.getHours() + seperator2 + date.getMinutes()+ seperator2 + date.getSeconds();return currentdate;}</script>  <body>  <input type="button" onclick="start()" value="开始"/></body>  </html> 

0 0
原创粉丝点击