一个JS倒计时函数

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><body>    
<script> 
function countDown(maxtime, fn) {
    var timer = setInterval(function() {
        if (maxtime >= 0) {
            minutes = Math.floor(maxtime / 60);
            seconds = Math.floor(maxtime % 60);
            msg = "距离结束还有" + minutes + "分" + seconds + "秒";
            fn(msg);
            if (maxtime == 5 * 60) alert('注意,还有5分钟!');
            --maxtime;
        } else {
            clearInterval(timer);
            fn("时间到,结束!");
        }
    },
    1000);
}
</script>
<div id="timer1" style="color:red"></div>
<div id="timer2" style="color:red"></div>
<script>
countDown( 6,function( msg ){document.getElementById('timer1').innerHTML = msg;});
countDown( 305,function( msg ){document.getElementById('timer2').innerHTML = msg;})</script>
</body></html>  
本博客所有文章如无特别注明均为原创。作者:sysdee复制或转载请以超链接形式注明转自 成功志
原文地址《一个JS倒计时函数
分享到:更多

相关推荐

发表评论

路人甲 表情
看不清楚?点图切换 Ctrl+Enter快速提交

网友评论(0)