javascript 不停(setInterval)/延时(setTimeout)函数
setTimeout(script,millisecond); 是从现在算起多少微秒后运行该代码(只运行一次)
setInterval(script,millisecond); 是每隔多少微秒运行一次代码
示例:
1.执行一次
<
span
id
="time"
></
span
>
秒
< script type ="text/javascript" >
var i = 1 ;
var txt = document.getElementById( " time " );
function addT(i) {
txt.innerHTML = i;
}
setTimeout(" addT(i++) " , 1000 );
</ script >
< script type ="text/javascript" >
var i = 1 ;
var txt = document.getElementById( " time " );
function addT(i) {
txt.innerHTML = i;
}
setTimeout(" addT(i++) " , 1000 );
</ script >
2.复制执行
<
span
id
="time"
>
3
</
span
>
秒
< script type ="text/javascript" >
var i = 1 ;
var txt = document.getElementById( " time " );
function addT(i) {
txt.innerHTML = i;
}
setInterval( " addT(i++) " , 1000 );
</ script >
< script type ="text/javascript" >
var i = 1 ;
var txt = document.getElementById( " time " );
function addT(i) {
txt.innerHTML = i;
}
setInterval( " addT(i++) " , 1000 );
</ script >
setTimeout也可以做成重复执行
<
span
id
="time"
></
span
>
秒
< script type ="text/javascript" >
var i = 1 ;
var txt = document.getElementById( " time " );
function addT(i) {
txt.innerHTML = i;
setTimeout( " addT(i++) " , 1000 );
}
addT(i);
</ script >
< script type ="text/javascript" >
var i = 1 ;
var txt = document.getElementById( " time " );
function addT(i) {
txt.innerHTML = i;
setTimeout( " addT(i++) " , 1000 );
}
addT(i);
</ script >