打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
jquery timer插件

jquery是当下最流行的javascript库了,在我们做前端特效的时候,定时操作是不可避免的,尤其是在开发响应式程序的时候, 会用到定时操作. 下面这个是基于jQuery的一个定时插件, 功能比较简单,但是确实很实用.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// JavaScript Document  
/**  
 * 作者:雪狐博客  
 * 实例:  
 * 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  
 *      }  
 * 可以定制自己想要的单位  
 *   
 **/
                
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 < 0)  
                return;  
                
            if (typeof times != 'number' || isNaN(times) || times < 0)   
                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);  
    });  
});

手机收藏该文章

标题: 基于jQuery的定时器插件

作者: 雪狐博客

出处: http://www.xuehuwang.com/read-435.html

更多: jQuery

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
常用JQuery插件整理
Ampersand.js
Jquery插件珍藏
关于jquery引用
jQuery的插件列表
jquery获取select选择的显示值
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服