复制代码 代码如下:
/**
* 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/10/16
*
* @author blair mitchelmore
* @version 1.2
*
**/
jquery.fn.extend({
everytime: function(interval, label, fn, times) {
return this.each(function() {
jquery.timer.add(this, interval, label, fn, times);
});
},
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.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) {
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 return;
if (typeof times != 'number' || isnan(times) || times times = 0;
times = times || 0;
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 ((++counter > times && times !== 0) || fn.call(element, counter) === false)
jquery.timer.remove(element, label, fn);
};
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);
});
});
js code
复制代码 代码如下:
$(#close-button).click(function() {
$(this).onetime(1000, function() {
$(this).parent(.main-window).hide();
});
});
$(#cancel-button).click(function() {
$(#close-button).stoptime();
});
jquery timers插件地址:
http://plugins.jquery.com/project/timers
下面来自javaeye论坛的jquery timers应用知识
提供了三个函式
1. everytime(时间间隔, [计时器名称], 函式名称, [次数限制], [等待函式程序完成])
2. onetime(时间间隔, [计时器名称], 呼叫的函式)
3. stoptime ([计时器名称], [函式名称])
复制代码 代码如下:
/*************************************************************
* everytime(时间间隔, [计时器名称], 函式名称, [次数限制], [等待函式程序完成])
*************************************************************/
//每1秒执行函式test()
function test(){
//do something...
}
$('body').everytime('1s',test);
//每1秒执行
$('body').everytime('1s',function(){
//do something...
});
//每1秒执行,并命名计时器名称为a
$('body').everytime('1s','a',function(){
//do something...
});
//每20秒执行,最多5次,并命名计时器名称为b
$('body').everytime('2das','b',function(){
//do something...
},5);
//每20秒执行,无限次,并命名计时器名称为c
//若时间间隔抵到,但函式程序仍未完成则需等待执行函式完成后再继续计时
$('body').everytime('2das','c',function(){
//执行一个会超过20秒以上的程式
},0,true);
/***********************************************************
* onetime(时间间隔, [计时器名称], 呼叫的函式)
***********************************************************/
//倒数10秒后执行
$('body').onetime('1das',function(){
//do something...
});
//倒数100秒后执行,并命名计时器名称为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
}
可以定制自己想要的了!
