美文网首页javascript
简单的jQuery定时器使用方法

简单的jQuery定时器使用方法

作者: 关保民 | 来源:发表于2017-08-11 10:29 被阅读0次

PS:非原创,仅为自己学习使用方便。。。

jQuery定时器

下面介绍一个用jQuery实现JS中的定时器方法,在jQuery中的jQuery Timers插件中已封装JS的setTimeout 和 setInterval 方法.
在jQuery Timers中提供了3个函数式

1.everyTime(时间间隔, [计时器名称], 函式名称, [次数限制], [等待函式程序完成])

2.oneTime(时间间隔, [计时器名称], 呼叫的函式)

3.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);
 });
});
原文链接:http://www.jianshu.com/p/6a8ce88c7f02
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

注:自定义时间单位,可以定制自己想要的单位.

powers: {   
// Yeah this is major overkill...  
  'ms': 1,   
  'cs': 10, 
  'ds': 100,   
  's': 1000,   
  'das': 10000,   
  'hs': 100000,  
  'ks': 1000000
}

原文链接:http://www.jianshu.com/p/6a8ce88c7f02
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

下载地址:http://download.csdn.net/detail/qq_32427791/9370474

相关文章

  • 简单的jQuery定时器使用方法

    PS:非原创,仅为自己学习使用方便。。。 jQuery定时器 下面介绍一个用jQuery实现JS中的定时器方法,在...

  • 简单的jQuery定时器使用方法

    jQuery定时器 下面介绍一个用jQuery实现JS中的定时器方法,在jQuery中的jQuery Timers...

  • 2018-07-19

    几个简单的jQuery使用方法 jQuery是JavaScript的一个集成库,语法简洁,它紧密集成了DOM,提供...

  • 初识js下的定时器

    定时器 setTimeout、setInterval 定时器的使用方法 setTimeout(fn,1000) ...

  • VUE中定时器如何使用?

    定时器的创建和使用 定时器的销毁 定时器 setTimeout()方法 和 setInterval() 使用方法相...

  • jquery表单验证插件

    jquery的validate插件前置知识:默认校验规则 使用方法:按顺序引入jquery.js、jquery.v...

  • 小总结1

    1.防止定时器累加 在设置定时器前先清除定时器 2.鼠标移上移开可用hover jquery 3.定时器缓动动画 ...

  • 第86节:Java中的JQuery基础

    第86节:Java中的JQuery 前言复习 定时器: 显示: 隐藏: 获取行 DOM: 什么是JQuery,有什...

  • 2018-01-11

    使用方法 引用jquery和jquery.lazyload.js到你的页面 | 1 2 | <``script s...

  • Linux Timer 示例

    定时器在日常编程中被频繁用到, 很多上层库都提供了很简单的使用方法, 如 glibc 中 g_timeout_ad...

网友评论

    本文标题:简单的jQuery定时器使用方法

    本文链接:https://www.haomeiwen.com/subject/xillrxtx.html