js實現100秒倒計時和簡易時鐘

來源:互聯網
上載者:User

標籤:char   1.5   point   window   .class   ==   style   ret   list   

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>100s倒計時</title>

<style>

body,div{margin:0;padding:0;}

body{color:#fff;font:16px/1.5 \5fae\8f6f\96c5\9ed1;}

#countdown{width:300px;text-align:center;background:#000;margin:10px auto;padding:20px 0;}

input{border:0;width:283px;height:50px;cursor:pointer;margin-top:20px;background:url(
) no-repeat;}

input.cancel{background-position:0 -50px;}

span{color:#000;width:80px;line-height:2;background:#fbfbfb;border:2px solid #b4b4b4;margin:0 10px;padding:0 10px;}

    </style>

    <script>

    window.onload = function(){

    var oCount = document.getElementById(‘countdown‘),

       oInput = oCount.getElementsByTagName(‘input‘)[0],

       aSpan = oCount.getElementsByTagName(‘span‘),

       timer = null;

    oInput.addEventListener(‘click‘,function(){

    this.className == ‘‘?(timer = setInterval(updateTime,1000)):clearInterval(timer);

    this.className = this.className === ‘cancel‘?‘‘:‘cancel‘;

    },false);

    function format(a){

    return a.toString().replace(/^(\d)$/,‘0$1‘);

    }

    function updateTime(){

    //console.log(aSpan[0].innerHTML);

    var remain = parseInt(aSpan[0].innerHTML.replace(/^0/,‘‘))*60+parseInt(aSpan[1].innerHTML.replace(/^0/,‘‘));

    if (remain <= 0) {

    clearInterval(timer);

    return;

    }

    remain--;

    aSpan[0].innerHTML = format(parseInt(remain/60));

    aSpan[1].innerHTML = format(remain%60);

 

    }

    }

    </script>

</head>

<body>

<div id="countdown">

<span>01</span>分鐘<span>40</span>秒

<input type="button" value="" />

</div>

</body>

</html>

 歡迎加入618237474,找群主私聊,送海量學習資料免費送

js實現100秒倒計時和簡易時鐘

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.