js setInterval與js setTimeout的用法

字號(hào):


    setTimeout
    定義和用法: setTimeout()方法用于在指定的毫秒數(shù)后調(diào)用函數(shù)或計(jì)算表達(dá)式。
    語法: setTimeout(code,millisec)
    參數(shù):
    code (必需):要調(diào)用的函數(shù)后要執(zhí)行的 JavaScript 代碼串。
    millisec(必需):在執(zhí)行代碼前需等待的毫秒數(shù)。
    提示: setTimeout() 只執(zhí)行 code 一次。如果要多次調(diào)用,請(qǐng)使用 setInterval() 或者讓 code 自身再次調(diào)用 setTimeout()。
    兩種調(diào)用函數(shù)的寫法:
    function page_list(){
    alert("shihuan");
    }
    window.setTimeout(page_list, 5000); //表示延時(shí)5秒執(zhí)行page_list()函數(shù)
    window.setTimeout("page_list()", 30000); //表示延時(shí)30秒執(zhí)行page_list()函數(shù)
    帶參數(shù)方法使用setTimeout要注意,setTimeout("函數(shù)名("+參數(shù)+")",毫秒數(shù)),這里的參數(shù)只能是字符串形式的,而不能傳遞一個(gè)對(duì)象,網(wǎng)上很多朋友也在問此類問題,我在此說明下,以下我舉幾個(gè)個(gè)簡(jiǎn)單的例子:網(wǎng)上查找下“帶參數(shù) setTimeout”,很多朋友寫了很多方法來實(shí)現(xiàn)使用setTimeout帶對(duì)象的方法循環(huán),例如: <script language="javascript">
    var __sto = setTimeout;
    window.setTimeout = function(callback, timeout, param) {
    var args = Array.prototype.slice.call(arguments, 2);
    var _cb = function() {
    callback.apply(null, args);
    } __sto(_cb, timeout); }
    //測(cè)試代碼
    function shihuan(a) {
    alert(a);
    }
    function yushibo(a, b, c) {
    alert(a + b + c);
    }
    var a = new Object();
    window.setTimeout(shihuan, 1000, a);
    window.setTimeout(yushibo, 2000, a, 6, 7);
    </script>
    此例中,setTimeout用法,setTimeout(回調(diào)函數(shù), 時(shí)間, 參數(shù)1, ..., 參數(shù)n)。
    --------------------------------------------------------------------------------------------------
    setInterval定義和用法
    setInterval() 方法可按照指定的周期(以毫秒計(jì))來調(diào)用函數(shù)或計(jì)算表達(dá)式。
    setInterval() 方法會(huì)不停地調(diào)用函數(shù),直到 clearInterval() 被調(diào)用或窗口被關(guān)閉。由 setInterval() 返回的 ID 值可用作 clearInterval() 方法的參數(shù)。
    語法
    setInterval(code,millisec[,"lang"])
    參數(shù)描述
    code必需。要調(diào)用的函數(shù)或要執(zhí)行的代碼串。
    millisec必須。周期性執(zhí)行或調(diào)用 code 之間的時(shí)間間隔,以毫秒計(jì)。
    返回值
    一個(gè)可以傳遞給 Window.clearInterval() 從而取消對(duì) code 的周期性執(zhí)行的值。
    例子:
    <html>
    <body>
    <form>
    <input type="text" id="clock" size="35" />
    <script language=javascript>
    var int=self.setInterval("clock()", 50)
    function clock(){
    var t=new Date()
    document.getElementByIdx_x_xx_x_x_x_x_x("clock").value = t
    }
    </script>
    </form>
    <button onclick="int=window.clearInterval(int)">Stop interval</button>
    </body>
    </html>
    --------------------------------------------------------------------------------------------------
    clearTimeout()和clearInterval() :
    JS里設(shè)定延時(shí):
    使用SetInterval和設(shè)定延時(shí)函數(shù)setTimeout 很類似。
    setTimeout 運(yùn)用在延遲一段時(shí)間,再進(jìn)行某項(xiàng)操作。
    setTimeout("function",time) 設(shè)置一個(gè)超時(shí)對(duì)象
    setInterval("function",time) //設(shè)置一個(gè)超時(shí)對(duì)象
    SetInterval為自動(dòng)重復(fù),setTimeout不會(huì)重復(fù)。
    clearTimeout(對(duì)象) 清除已設(shè)置的setTimeout對(duì)象
    clearInterval(對(duì)象) 清除已設(shè)置的setInterval對(duì)象
    一、什么叫做JavaScript 的計(jì)時(shí)事件
    使用JavaScript可以實(shí)現(xiàn)代碼的延時(shí)執(zhí)行,也就是說當(dāng)一個(gè)函數(shù)被調(diào)用時(shí)不立即執(zhí)行某些代碼,而是等一段指定的時(shí)間后再執(zhí)行,這就叫做計(jì)時(shí)事件。
    二、JavaScript 的計(jì)時(shí)事件的函數(shù)
    setTimeout() //- 在指定時(shí)間后執(zhí)行代碼
    clearTimeout() //- 取消 setTimeout()
    注意: setTimeout() 和 clearTimeout() 都是HTML DOM 的 Window 對(duì)象的函數(shù)。
    三、setTimeout詳解
    var t = setTimeout("javascript 語句", 時(shí)間參數(shù))
    注:時(shí)間參數(shù)單位為毫秒
    示例:var t=setTimeout("alert('3 seconds!')", 3000)
    如果js語句帶變量,則必須用+號(hào)將變量連接起來,如:
    var t = setTimeout("document.getElementByIdx_x_xx_x_x_x_x_x("+menuid+").style.display='none'", 3000)
    四、clearTimeout詳解
    語法:clearTimeout(setTimeout的變量名)
    示例:clearTimeout(t) //其中t為前面設(shè)置的setTimeout的變量
    使用clearTimeout可以隨時(shí)停止計(jì)時(shí)。
    五、應(yīng)用技巧
    建議將setTimeout單獨(dú)設(shè)置為一個(gè)函數(shù)。如:
    function delayRun(code, time) {
    var t = setTimeout(code, time);
    }
    這樣,在需要讓某段代碼延時(shí)執(zhí)行的時(shí)候,只需在這段代碼前加入這個(gè)函數(shù)就可以了。如:
    onmouseover = delayRun("setTab(0,0)", 500)
    其中setTab是一個(gè)自定義的函數(shù)。如果以后不想讓setTab延時(shí)執(zhí)行,則去掉語句中的delayRun相關(guān)的代碼即可,
    改為:onmouseover=setTab(0, 0) 就可以了。
    這種寫法避免每一個(gè)需要延時(shí)的地方都寫一段setTimeout的代碼,只需要直接調(diào)用就可以了,很方便。也節(jié)省了代碼的量。