setinterval()與clearInterval()JS函數的調用方法,setinterval

來源:互聯網
上載者:User

setinterval()與clearInterval()JS函數的調用方法,setinterval

本文執行個體講述了setinterval()與clearInterval()JS函數的調用方法。分享給大家供大家參考。具體如下:

複製代碼 代碼如下:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title>setinterval()與clearInterval()用法</title> 
    <script type="text/javascript"> 
        function f1() { 
            alert("調用f1"); 
        } 
        function f2() { 
            alert("調用f2"); 
        } 
 
        function bodymousedown() { 
            alert("你好"); 
            alert("我好"); 
 
        } 
        function com() { 
            if (confirm("是否進入")) { 
                alert("進入了"); 
            } 
            else { 
                alert("退出"); 
            } 
        } 
        var interval; 
        function getinterval() { 
            if (confirm("確定要執行嗎?")) { 
                 
                interval = setInterval("alert('每隔2000毫秒執行一次')", 2000); 
            } 
            else { 
                alert("不執行"); 
            } 
        } 
        function setTimeOut1() { 
            setTimeout("alert('3000毫秒後執行這段代碼')", 3000); 
        } 
 
    </script> 
</head> 
<!--" -->  
<!--<body onmousedown ="bodymousedown()">--> 
<body> 
<!--ondblclick是雙擊事件,onclick是單擊事件--> 
<input type="button" onclick="document.ondblclick=f1" value="關聯事件1" />
<input type="button" onclick="document.ondblclick=f2" value="關聯事件2" />
 
<input type="button" ondblclick="bodymousedown()" value="調用函數" /> 
<input type="button" onclick="com()" value="confirm的用法" /> 
<input type="button" onclick="getinterval()" value="setInterval的用法,每隔一段時間執行指定的代碼" /> 
 
<!--clearInterval取消setInterval的定時執行,相當於Timer中的Enabled=False--> 
<input type="button" onclick="clearInterval(interval)" value="取消執行setinterval代碼" /> 
 
<input type="button" onclick="setTimeOut1()" value="setTimeOut,某個時間執行代碼" /> 
 
</body> 
</html>

setInterval() 方法可按照指定的周期(以毫秒計)來調用函數或計算運算式。
setInterval() 方法會不停地調用函數,直到 clearInterval() 被調用或視窗被關閉。由 setInterval() 返回的 ID 值可用作 clearInterval() 方法的參數。
文法
複製代碼 代碼如下:setInterval(code,millisec[,"lang"])

參數 描述
code 必需。要調用的函數或要執行的代碼串。
millisec 必須。周期性執行或調用 code 之間的時間間隔,以毫秒計。

setTimeout只運行一次,也就是說設定的時間到後就觸發運行指定代碼,運行完後即結束。
setinterval是迴圈啟動並執行,即每到設定時間間隔就觸發指定代碼。這是真正的定時器。

希望本文所述對大家的javascript程式設計有所協助。

聯繫我們

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