window.clearInterval與window.setInterval的用法 定時器的設定

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   ar   java   sp   div   

window.setInterval()

功能:按照指定的周期(以毫秒計)來調用函數或計算運算式。

文法:setInterval(code,millisec) 

    code:在指定時間到時要執行的JavaScript代碼串或函數。

    millisec:設定的定時時間,用毫秒數表示。

傳回值:定時器的ID值,可用於clearInterval()方法停止指定的定時器。

註:setInterval()方法會不停地調用函數,直到用clearInterval()終止定時或視窗被關閉。

window.clearInterval()

功能:取消由setInterval()方法設定的定時器。

文法:clearInterval(id_of_setinterval)

解釋:id_of_setinterval:由setInterval()返回的ID值。該值標識了一個setInterval定時器。

也就是:window.setInterval()返回的就是window.clearInterval的參數

例如:

  

<div class="cg_content">    <div class="success">        <img src="images/success.png">        <span>恭喜您 註冊成功!</span>    </div>    <p>頁面將在 <b>5</b> 秒內自動跳轉到<a href="javascript:void(0)" onclick="tiger();"> 首頁</a></p></div><div class="zc_footer"></div><script type="text/javascript" src="jquery-1.11.1.min.js"></script><script type="text/javascript">    $(document).ready(function(){        var i = 5;  設定為5秒        var f = function(){            i--;            $(".cg_content p b").text(i);            if(i == 0){                clearInterval(screen_time); 當等於0時,計時結束,進行跳轉                window.location=‘http://www.baidu.com‘;            }            alert(screen_time);        }        var screen_time = setInterval(f,1000); 計時執行,一秒執行一次函數    });</script><script type="text/javascript">    function tiger(){        window.location=‘http://www.sina.com‘;    }</script>

 

window.clearInterval與window.setInterval的用法 定時器的設定

聯繫我們

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