html中倒計時功能(setInterval,clearInterval)

來源:互聯網
上載者:User

標籤:trap   http   https   head   star   str   提交   val   lin   

倒計時主要用到的知識點:1、設定時間間隔的setInterval可以被clearInterval取消

            2、毫秒轉換為時分格式

  下面是js中的函數

  var shijian=3600;    var time=null;    function start(){         time=setInterval("count()",1000);//傳回值time知識為了需要暫停時候clearInterval(time)清除時間間隔    }        function count(){        if(shijian<0){            alert("time out");            pause();//一般情況下這個if條件是用來提交資料用的,這裡只是測試一下。        }else{            $("#time p").html(Math.floor(shijian/60)+":"+shijian%60);//這裡用到將毫秒轉換到時分格式            shijian--;            console.info(time);        }    }    function pause(){        clearInterval(time);    }    function goOn(){        time = setInterval("count()",1000);//重新設定時間間隔    }

第二個是html資源,為了方便我css直接寫在html中了

<!doctype html><html lang="en"><head>    <meta charset="UTF-8">    <title>Document</title>    <script src="1.js"></script>    <script src="jquery-1.8.3.min.js"></script>    <style type="text/css">        input{            background-color: #9fc5f1;            width: 100px;             height: 30px;             line-height: 30px;             text-align: center;             color: #fff;             font-size:14px;             font-weight: bold;        }        #time p{            color: #1f6dc2;            font-size: 48px;             font-weight: bold;            margin-left:90px;            margin-bottom:0px;        }    </style>    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"><body><div id="time">    <p>00:00</p>    <input type="button" value="開始" onclick="start()"/>    <input type="button" value="暫停" onclick="pause()"/>    <input type="button" value="繼續" onclick="goOn()"/></div></body></html>

需要代碼的小夥伴可以自行下載

連結:http://pan.baidu.com/s/1boZ8Xun 密碼:ymu0

html中倒計時功能(setInterval,clearInterval)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.