js多set Interval衝突の解決方法
1733 ワード
<!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>