js倒计时跳转jquery插件版 猫

 1 <script type="text/javascript" src="js/jquery1.91.min.js"></script>
 2          
 3 <script type="text/javascript">
 4              
 5         
 6       ;(function(){
 7 
 8             $.fn.count=function(options){
 9                      
10                      var defaults={
11                          'time':'30',
12                          'url':'http://www.baidu.com'
13                          
14                      };
15                      
16                      var settings=$.extend({},defaults,options);
17                      var timer=null;
18                      var _this=this;
19                      this.html(settings.time);
20                     
21                      function runNewpage(){
22                        if(settings.time<=0){
23                            clearInterval(timer);
24                           window.location.href=settings.url;
25                          
26                        }
27                        _this.html(settings.time);
28                        settings.time--;
29                      }
30                      
31                      timer=setInterval(runNewpage,1000);
32                      
33                  }
34                  
35          })();
36              
37              
38 
39 $(function(){
40         
41         
42         $('#number').count({
43             time:3,
44             url:'http://www.aliyun.com'
45             
46         });
47  
48         
49         
50 });

要注意一点,this指向问题

函数外部指向对象本身,就是谁调用属于谁,这里属于$('#number');

函数内部指向window  需要转存一下。

原文地址:https://www.cnblogs.com/simao/p/7241188.html