求js大神指点迷津!解决办法
求js大神指点迷津!
[size=16px][size=14px]<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div id="myDate"></div>
<script>
var dd = new Array("一","二","三","四","五","六","日");
var timer;
function show_date(dateEle){
var currDate = new Date();
var time;
time = currDate.getFullYear() + "年";
time += (currDate.getMonth()+1) + "月";
time += currDate.getDate() + "日" + " ";
time += "星期" + dd[currDate.getDay()-1] + " ";
time += addzero(currDate.getHours()) + ":";
time += addzero(currDate.getMinutes()) + ":";
time += addzero(currDate.getSeconds());
dateEle.innerHTML = time;
timer = setTimeout(function(){show_date.call(this,dateEle);},1000);
}
function addzero(i){
if(i<10){
return "0" + i;
}
return i;
}
window.onload = function(){
if(document.getElementById("myDate")){
var myDateEle = document.getElementById("myDate");
show_date(myDateEle);
}
};
</script>
</body>
</html>[/size][/size]
这是一个js时间自动跳秒的代码,其他的我都能看懂,就是上面加红的那段代码不明白,为什么要那样写?
因为动画不是setInterval方法吗? 怎么是setTimeout了?
并且重点解释一下function(){show_date.call(this,dateEle); 这个是什么意思?
------解决思路----------------------
你打印一下this,这是调用自身的,递归。。。
[size=16px][size=14px]<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div id="myDate"></div>
<script>
var dd = new Array("一","二","三","四","五","六","日");
var timer;
function show_date(dateEle){
var currDate = new Date();
var time;
time = currDate.getFullYear() + "年";
time += (currDate.getMonth()+1) + "月";
time += currDate.getDate() + "日" + " ";
time += "星期" + dd[currDate.getDay()-1] + " ";
time += addzero(currDate.getHours()) + ":";
time += addzero(currDate.getMinutes()) + ":";
time += addzero(currDate.getSeconds());
dateEle.innerHTML = time;
timer = setTimeout(function(){show_date.call(this,dateEle);},1000);
}
function addzero(i){
if(i<10){
return "0" + i;
}
return i;
}
window.onload = function(){
if(document.getElementById("myDate")){
var myDateEle = document.getElementById("myDate");
show_date(myDateEle);
}
};
</script>
</body>
</html>[/size][/size]
这是一个js时间自动跳秒的代码,其他的我都能看懂,就是上面加红的那段代码不明白,为什么要那样写?
因为动画不是setInterval方法吗? 怎么是setTimeout了?
并且重点解释一下function(){show_date.call(this,dateEle); 这个是什么意思?
------解决思路----------------------
你打印一下this,这是调用自身的,递归。。。