js setTimeout

setTimeout用法

//每个0.5秒钟改变字体和背景颜色,字体一闪一闪的效果
var flag = 0;
 function start(){
    var text = document.getElementById("time");
    if (!flag)
    {
        text.style.color = "red";
        text.style.background = "#0000ff";
        flag = 1;
    }else{
        text.style.color = "";
        text.style.background = "";
        flag = 0;
    }
    setTimeout("start()",500);
 }

启动定时,结束定时

//定时开始
t=setTimeout("showTime()",1000);
//定时结束
clearTimeout(t);

倒计时

var time=10;    
function showTime(){
    time-=1;
    $('#time').html(time);
    if (time==0){
            return false;
    }else{
        setTimeout("showTime()",1000);  
    }
    
}
原文地址:https://www.cnblogs.com/blueskycc/p/5261697.html