基于JQuery.timer插件實(shí)現(xiàn)一個(gè)計(jì)時(shí)器
更新時(shí)間:2010年04月25日 16:49:37 作者:
基于JQuery.timer插件實(shí)現(xiàn)一個(gè)計(jì)時(shí)器,需要的朋友可以參考下。
先去官網(wǎng)下載jQuery Timers插件 ,然后引用到html中。這里是1.2 version
<script src="../Javascripts/Plugins/jquery.timers-1.2.js" type="text/javascript"></script>
然后是HTML,我們可以放一個(gè)hidden 的server control存值用,當(dāng)然這個(gè)隨你了。
<asp:HiddenField ID="hicurrenttime" runat="server" />
<h1>
jQuery Timers Test</h1>
<input type="button" id="btnmaster" value="StartTimer" />
<h2>
Demos</h2>
<div class="demos">
<span id="durationtimerspan"></span>
<br />
<input id="txtresult" type="text" />
</div>
加上JS:
[/code]
$(document).ready(function() {
var countnum = <%=hicurrenttime.Value %>;
$('#btnmaster').toggle(
function() {
$(this).val('StopTimer');
$('#durationtimerspan').everyTime(1000, 'testtimer', function(i) {
countnum = countnum + 1;
$(this).html('Duration: ' + countnum);
$('#<%=hicurrenttime.ClientID %>').val(countnum);
});
},
function() {
$(this).val('StartTimer');
$('#durationtimerspan').stopTime('testtimer');
$('#txtresult').val(countnum);
});
});
[html]
上面的代碼關(guān)鍵的地方是我們用toggle函數(shù),去實(shí)現(xiàn)點(diǎn)擊Button開關(guān)計(jì)時(shí)器。這個(gè)插件有三個(gè)方法:
everyTime(interval : Integer | String, [label = interval : String], fn : Function, [times = 0 : Integer])
每次都執(zhí)行
oneTime(interval : Integer | String, [label = interval : String], fn : Function)
執(zhí)行一次
stopTime([label : Integer | String], [fn : Function])
停止
最后我們效果如下圖:
類似的用法:
//每1秒執(zhí)行函式test()
function test(){
//do something...
}
$('body').everyTime('1s',test);
//每1秒執(zhí)行
$('body').everyTime('1s',function(){
//do something...
});
//每1秒執(zhí)行,并命名計(jì)時(shí)器名稱為A
$('body').everyTime('1s','A',function(){
//do something...
});
//每20秒執(zhí)行,最多5次,并命名計(jì)時(shí)器名稱為B
$('body').everyTime('2das','B',function(){
//do something...
},5);
//每20秒執(zhí)行,無(wú)限次,并命名計(jì)時(shí)器名稱為C
//若時(shí)間間隔抵到,但函式程序仍未完成則需等待執(zhí)行函式完成后再繼續(xù)計(jì)時(shí)
$('body').everyTime('2das','C',function(){
//執(zhí)行一個(gè)會(huì)超過(guò)20秒以上的程式
},0,true);
/***********************************************************
* oneTime(時(shí)間間隔, [計(jì)時(shí)器名稱], 呼叫的函式)
***********************************************************/
//倒數(shù)10秒后執(zhí)行
$('body').oneTime('1das',function(){
//do something...
});
//倒數(shù)100秒后執(zhí)行,并命名計(jì)時(shí)器名稱為D
$('body').oneTime('1hs','D',function(){
//do something...
});
/************************************************************
* stopTime ([計(jì)時(shí)器名稱], [函式名稱])
************************************************************/
//停止所有的在$('body')上計(jì)時(shí)器
$('body').stopTime ();
//停止$('body')上名稱為A的計(jì)時(shí)器
$('body').stopTime ('A');
//停止$('body')上所有呼叫test()的計(jì)時(shí)器
$('body').stopTime (test);
希望這篇POST對(duì)您有幫助。Author: Petter Liu
復(fù)制代碼 代碼如下:
<script src="../Javascripts/Plugins/jquery.timers-1.2.js" type="text/javascript"></script>
然后是HTML,我們可以放一個(gè)hidden 的server control存值用,當(dāng)然這個(gè)隨你了。
復(fù)制代碼 代碼如下:
<asp:HiddenField ID="hicurrenttime" runat="server" />
<h1>
jQuery Timers Test</h1>
<input type="button" id="btnmaster" value="StartTimer" />
<h2>
Demos</h2>
<div class="demos">
<span id="durationtimerspan"></span>
<br />
<input id="txtresult" type="text" />
</div>
加上JS:
[/code]
$(document).ready(function() {
var countnum = <%=hicurrenttime.Value %>;
$('#btnmaster').toggle(
function() {
$(this).val('StopTimer');
$('#durationtimerspan').everyTime(1000, 'testtimer', function(i) {
countnum = countnum + 1;
$(this).html('Duration: ' + countnum);
$('#<%=hicurrenttime.ClientID %>').val(countnum);
});
},
function() {
$(this).val('StartTimer');
$('#durationtimerspan').stopTime('testtimer');
$('#txtresult').val(countnum);
});
});
[html]
上面的代碼關(guān)鍵的地方是我們用toggle函數(shù),去實(shí)現(xiàn)點(diǎn)擊Button開關(guān)計(jì)時(shí)器。這個(gè)插件有三個(gè)方法:
everyTime(interval : Integer | String, [label = interval : String], fn : Function, [times = 0 : Integer])
每次都執(zhí)行
oneTime(interval : Integer | String, [label = interval : String], fn : Function)
執(zhí)行一次
stopTime([label : Integer | String], [fn : Function])
停止
最后我們效果如下圖:

類似的用法:
復(fù)制代碼 代碼如下:
//每1秒執(zhí)行函式test()
function test(){
//do something...
}
$('body').everyTime('1s',test);
//每1秒執(zhí)行
$('body').everyTime('1s',function(){
//do something...
});
//每1秒執(zhí)行,并命名計(jì)時(shí)器名稱為A
$('body').everyTime('1s','A',function(){
//do something...
});
//每20秒執(zhí)行,最多5次,并命名計(jì)時(shí)器名稱為B
$('body').everyTime('2das','B',function(){
//do something...
},5);
//每20秒執(zhí)行,無(wú)限次,并命名計(jì)時(shí)器名稱為C
//若時(shí)間間隔抵到,但函式程序仍未完成則需等待執(zhí)行函式完成后再繼續(xù)計(jì)時(shí)
$('body').everyTime('2das','C',function(){
//執(zhí)行一個(gè)會(huì)超過(guò)20秒以上的程式
},0,true);
/***********************************************************
* oneTime(時(shí)間間隔, [計(jì)時(shí)器名稱], 呼叫的函式)
***********************************************************/
//倒數(shù)10秒后執(zhí)行
$('body').oneTime('1das',function(){
//do something...
});
//倒數(shù)100秒后執(zhí)行,并命名計(jì)時(shí)器名稱為D
$('body').oneTime('1hs','D',function(){
//do something...
});
/************************************************************
* stopTime ([計(jì)時(shí)器名稱], [函式名稱])
************************************************************/
//停止所有的在$('body')上計(jì)時(shí)器
$('body').stopTime ();
//停止$('body')上名稱為A的計(jì)時(shí)器
$('body').stopTime ('A');
//停止$('body')上所有呼叫test()的計(jì)時(shí)器
$('body').stopTime (test);
希望這篇POST對(duì)您有幫助。Author: Petter Liu
您可能感興趣的文章:
- 使用jquery讀取html5 localstorage的值的方法
- jQuery訪問(wèn)瀏覽器本地存儲(chǔ)cookie、localStorage和sessionStorage的基本用法
- jQuery timers計(jì)時(shí)器簡(jiǎn)單應(yīng)用說(shuō)明
- jquery 顯示*天*時(shí)*分*秒實(shí)現(xiàn)時(shí)間計(jì)時(shí)器
- 基于jquery插件編寫countdown計(jì)時(shí)器
- jQuery實(shí)現(xiàn)簡(jiǎn)單的計(jì)時(shí)器功能實(shí)例分析
- sliderToggle在寫jquery的計(jì)時(shí)器setTimeouter中不生效
- jQuery實(shí)現(xiàn)倒計(jì)時(shí)功能 jQuery實(shí)現(xiàn)計(jì)時(shí)器功能
- jquery實(shí)現(xiàn)一個(gè)全局計(jì)時(shí)器(商城可用)
- 利用jQuery+localStorage實(shí)現(xiàn)一個(gè)簡(jiǎn)易的計(jì)時(shí)器示例代碼
相關(guān)文章
jquery加載頁(yè)面的方法(頁(yè)面加載完成就執(zhí)行)
jquery加載頁(yè)面的方法(頁(yè)面加載完成就執(zhí)行),建議大家看下windows.onload與$(document).ready之間的區(qū)別。2011-06-06動(dòng)態(tài)設(shè)置form表單的action屬性的值的簡(jiǎn)單方法
下面小編就為大家?guī)?lái)一篇?jiǎng)討B(tài)設(shè)置form表單的action屬性的值的簡(jiǎn)單方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-05-05jquery+正則實(shí)現(xiàn)統(tǒng)一的表單驗(yàn)證
表單驗(yàn)證一直很繁瑣,特別是大點(diǎn)的表單,如果每個(gè)input都去單獨(dú)寫驗(yàn)證簡(jiǎn)直要寫死人,最近寫了一小段js統(tǒng)一的驗(yàn)證表單內(nèi)容是否正確。需要的朋友可以參考下2015-09-09ASP.NET jQuery 實(shí)例12 通過(guò)使用jQuery validation插件簡(jiǎn)單實(shí)現(xiàn)用戶注冊(cè)頁(yè)面驗(yàn)證功能
在這節(jié)我們將使用validation插件實(shí)現(xiàn)注冊(cè)頁(yè)面的驗(yàn)證功能,通過(guò)這個(gè)例子,可以更全面的掌握該插件的使用功能2012-02-02jquery手機(jī)觸屏滑動(dòng)拼音字母城市選擇器的實(shí)例代碼
下面小編就為大家分享一篇jquery手機(jī)觸屏滑動(dòng)拼音字母城市選擇器的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12jQuery處理xml格式的返回?cái)?shù)據(jù)(實(shí)例解析)
在以下示例中,我將使用$.ajax()方法,使用$.get()方法也可以,但我覺得$.ajax()更好一些,代碼更容易懂,并且也不怎么復(fù)雜2013-11-11