JQuery Timers應用知識
提供了三個函式
1. everyTime(時間間隔, [計時器名稱], 函式名稱, [次數(shù)限制], [等待函式程序完成])
2. oneTime(時間間隔, [計時器名稱], 呼叫的函式)
3. stopTime ([計時器名稱], [函式名稱])
/*************************************************************
* everyTime(時間間隔, [計時器名稱], 函式名稱, [次數(shù)限制], [等待函式程序完成])
*************************************************************/
//每1秒執(zhí)行函式test()
function test(){
//do something...
}
$('body').everyTime('1s',test);
//每1秒執(zhí)行
$('body').everyTime('1s',function(){
//do something...
});
//每1秒執(zhí)行,并命名計時器名稱為A
$('body').everyTime('1s','A',function(){
//do something...
});
//每20秒執(zhí)行,最多5次,并命名計時器名稱為B
$('body').everyTime('2das','B',function(){
//do something...
},5);
//每20秒執(zhí)行,無限次,并命名計時器名稱為C
//若時間間隔抵到,但函式程序仍未完成則需等待執(zhí)行函式完成后再繼續(xù)計時
$('body').everyTime('2das','C',function(){
//執(zhí)行一個會超過20秒以上的程式
},0,true);
/***********************************************************
* oneTime(時間間隔, [計時器名稱], 呼叫的函式)
***********************************************************/
//倒數(shù)10秒后執(zhí)行
$('body').oneTime('1das',function(){
//do something...
});
//倒數(shù)100秒后執(zhí)行,并命名計時器名稱為D
$('body').oneTime('1hs','D',function(){
//do something...
});
/************************************************************
* stopTime ([計時器名稱], [函式名稱])
************************************************************/
//停止所有的在$('body')上計時器
$('body').stopTime ();
//停止$('body')上名稱為A的計時器
$('body').stopTime ('A');
//停止$('body')上所有呼叫test()的計時器
$('body').stopTime (test);
自定義時間單位
打開源代碼
找到
powers: {
// Yeah this is major overkill...
'ms': 1,
'cs': 10,
'ds': 100,
's': 1000,
'das': 10000,
'hs': 100000,
'ks': 1000000
}
示例:
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JQuery定時器</title>
<script src="jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="jquery.timers-1.2.js" type="text/javascript"></script>
<script>
function check() {
$("#Button1").attr("disabled", true);
$("#Button1").val("正在提交,請稍等3秒.....");
$('body').oneTime('3s', function() {
$("#Button1").attr("disabled", false);
$("#Button1").val("測試提交");
alert("3秒后提交結(jié)果");
});
}
</script>
</head>
<body>
<input id="Button1" onclick="check();return false;" value="測試提交" type="submit" name="Button1">
</body>
</html>
!評論內(nèi)容需包含中文