SetTimeout:表示指定时间后执行某操作.(操作只会被执行一次).
SetInterval:表示每隔指定时间执行某操作.(操作会被不断的调用).
例:下操作是2秒后隐藏hide.
function Button1_onclick()
{
document.getElementById("hide").style.display = "inherit";
setTimeout(hideDiv, 2000);
}
function hideDiv()
{
document.getElementById("hide").style.display = "none";
}
{
document.getElementById("hide").style.display = "inherit";
setTimeout(hideDiv, 2000);
}
function hideDiv()
{
document.getElementById("hide").style.display = "none";
}
下面代码显示当前时间:
function Button1_onclick()
{
setInterval(showTimeNow, 1000);
}
function showTimeNow()
{
document.getElementById("lblTime").innerHTML = new Date().toLocaleTimeString();
}
{
setInterval(showTimeNow, 1000);
}
function showTimeNow()
{
document.getElementById("lblTime").innerHTML = new Date().toLocaleTimeString();
}