jQuery定时器
下面介绍一个用jQuery实现JS中的定时器方法,在jQuery中的jQuery Timers插件中已封装JS的setTimeout 和 setInterval 方法.
在jQuery Timers中提供了3个函数式
- everyTime(时间间隔, [计时器名称], 函式名称, [次数限制], [等待函式程序完成])
- oneTime(时间间隔, [计时器名称], 呼叫的函式)
- stopTime ([计时器名称], [函式名称])
everyTime函数式
everyTime函数式相当于js中的循环定时器
1:每间隔一秒钟循环执行a()
function a(){
alert(1);
}
$('body').everyTime('1s',a);
2:简写版
$('body').everyTime('1s',function(){
alert(1);
});
3:每隔一秒执行,并给计时器命名为A
$('body').everyTime('1s','A',function(){
alert(1);
});
4:每隔二十秒执行,最多5次,并给计时器命名为A
$('body').everyTime('20s','A',function(){
alert(1);
},5);
oneTime函数式
1:10s后执行函数
$('body').oneTime('1das',function(){
alert(1);
});
2:100s后执行函数,并给计时器命名为A
$('body').oneTime('100s','A',function(){
alert(1);
});
stopTime函数式
1:停止所有的在$('body')上计时器
$('body').stopTime ();
2:停止$('body')上名称为A的计时器
$('body').stopTime ('A');
3:停止$('body')上所有呼叫a()的计时器
$('body').stopTime (a);
jQuery Timers插件代码
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px Consolas; color: #a5b2b9}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px Consolas; min-height: 19.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px Consolas; color: #596972}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px Consolas; color: #b58a00}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px Consolas; color: #2eafa9}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px Consolas; color: #da6c34}p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px Consolas; color: #ad5cff}p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px Consolas; color: #97a700}p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px Consolas; color: #3c7400}p.p10 {margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px Consolas; color: #596972; min-height: 19.0px}p.p11 {margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px Consolas; color: #6b82d9}span.s1 {color: #596972}span.s2 {color: #6b82d9}span.s3 {color: #97a700}span.s4 {color: #b58a00}span.s5 {color: #ad5cff}span.s6 {color: #3c7400}span.s7 {color: #000000}span.s8 {color: #2eafa9}span.s9 {color: #d8a100}span.Apple-tab-span {white-space:pre}
/**
* jQuery.timers - Timer abstractions for jQuery
* Written by Blair Mitchelmore (blair DOT mitchelmore AT gmail DOT com)
* Licensed under the WTFPL (http://sam.zoy.org/wtfpl/).
* Date: 2009/02/08
*
* @author Blair Mitchelmore
* @version 1.1.2
*
**/
jQuery.fn.extend({
everyTime: function(interval, label, fn, times, belay) {
return this.each(function() {
jQuery.timer.add(this, interval, label, fn, times, belay);
});
},
oneTime: function(interval, label, fn) {
return this.each(function() {
jQuery.timer.add(this, interval, label, fn, 1);
});
},
stopTime: function(label, fn) {
return this.each(function() {
jQuery.timer.remove(this, label, fn);
});
}
});
jQuery.event.special
jQuery.extend({
timer: {
global: [],
guid: 1,
dataKey: "jQuery.timer",
regex: /^([0-9]+(?:\.[0-9]*)?)\s*(.*s)?$/,
powers: {
// Yeah this is major overkill...
'ms': 1,
'cs': 10,
'ds': 100,
's': 1000,
'das': 10000,
'hs': 100000,
'ks': 1000000
},
timeParse: function(value) {
if (value == undefined || value == null)
return null;
var result = this.regex.exec(jQuery.trim(value.toString()));
if (result[2]) {
var num = parseFloat(result[1]);
var mult = this.powers[result[2]] || 1;
return num * mult;
} else {
return value;
}
},
add: function(element, interval, label, fn, times, belay) {
var counter = 0;
if (jQuery.isFunction(label)) {
if (!times)
times = fn;
fn = label;
label = interval;
}
interval = jQuery.timer.timeParse(interval);
if (typeof interval != 'number' || isNaN(interval) || interval <= 0)
return;
if (times && times.constructor != Number) {
belay = !!times;
times = 0;
}
times = times || 0;
belay = belay || false;
var timers = jQuery.data(element, this.dataKey) || jQuery.data(element, this.dataKey, {});
if (!timers[label])
timers[label] = {};
fn.timerID = fn.timerID || this.guid++;
var handler = function() {
if (belay && this.inProgress)
return;
this.inProgress = true;
if ((++counter > times && times !== 0) || fn.call(element, counter) === false)
jQuery.timer.remove(element, label, fn);
this.inProgress = false;
};
handler.timerID = fn.timerID;
if (!timers[label][fn.timerID])
timers[label][fn.timerID] = window.setInterval(handler,interval);
this.global.push( element );
},
remove: function(element, label, fn) {
var timers = jQuery.data(element, this.dataKey), ret;
if ( timers ) {
if (!label) {
for ( label in timers )
this.remove(element, label, fn);
} else if ( timers[label] ) {
if ( fn ) {
if ( fn.timerID ) {
window.clearInterval(timers[label][fn.timerID]);
delete timers[label][fn.timerID];
}
} else {
for ( var fn in timers[label] ) {
window.clearInterval(timers[label][fn]);
delete timers[label][fn];
}
}
for ( ret in timers[label] ) break;
if ( !ret ) {
ret = null;
delete timers[label];
}
}
for ( ret in timers ) break;
if ( !ret )
jQuery.removeData(element, this.dataKey);
}
}
}
});
jQuery(window).bind("unload", function() {
jQuery.each(jQuery.timer.global, function(index, item) {
jQuery.timer.remove(item);
});
});
注:自定义时间单位,可以定制自己想要的单位.
powers: {
// Yeah this is major overkill...
'ms': 1,
'cs': 10,
'ds': 100,
's': 1000,
'das': 10000,
'hs': 100000,
'ks': 1000000
}
网友评论