淺析javascript 定時器,淺析javascript

來源:互聯網
上載者:User

淺析javascript 定時器,淺析javascript

setTimeout()--用於指定在一段特定的時間後執行某段程式。

          格式:

[定時器對象名=]setTimeout(“<運算式>”,毫秒數);

功能: 執行<運算式>一次。

其中運算式是字串,可以使任意javascript語句

複製代碼 代碼如下:
<html>
<head>
<script type="text/javascript">
//5秒之後執行alert
function count(){
    setTimeout("alert('執行成功');",5000);
}
</script>
</head>
<body>
<input type="button" value="執行" onclick="count()">
</body>
</html>

setInterval()—重複執行<運算式>,直至視窗、架構被關閉或執行clearInterval

格式: [定時器對象名=]setInterval(“<運算式>”,毫秒)

clearInterval() 終止定時器

格式: clearInterval(定時器對象名)

複製代碼 代碼如下:
<html>
<head>
<script type="text/javascript">
var sec=0;
var timeId=setInterval("count();",1000);

function count(){
    document.getElementById("num").innerHTML=sec++;
}

function stopCount(){
    clearInterval(timeId);
}
</script>
</head>
<body>
<font style="color:red" id="num">0</font>秒鐘<input type="button" value="停止" onclick="stopCount();">
</body>
</html>

是不是很好用呢,有需要的小夥伴參考下吧。

聯繫我們

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