+发表新主题
查看: 1709|回复: 0

[代码片段] BOM 一次性计时器

[复制链接]

[代码片段] BOM 一次性计时器

[复制链接]
殷三姗 发表于 2016-1-25 20:06:17 浏览:  1709 回复:  0 [显示全部楼层] |只看大图 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
1.一次性定时器:先等待一段时间,再自动执行一次任务,自动停止

      如何使用:3步:和周期性完全一样:

             1. 任务函数的结尾:何种情况下重新启动等待

                 2. 只要停,就要定义全局变量timer

             3. 启动:timer=setTimeout(taskFun,等待时间);

             4. 停止:clearTimeout(timer);

                                                                                     timer=null;


     何时需要留住this:对象的方法内又启动了定时器的回调

               方法中定义var self=this,留住this

              回调函数中:self.方法名();


例如:模拟页面右下角广告弹出框   

  1. <!DOCTYPE html>

  2. <html>

  3. <head>

  4.   <title>窗口右下角消息弹出框</title>

  5.   <meta charset="utf-8"/>

  6.   <style>

  7.    *{margin:0;padding:0}

  8.   #msg{width:200px;height:200px;

  9.         position:fixed;

  10.         right:30px;

  11.         bottom:-200px;

  12.         background-color:LightBlue;

  13. }

  14.    #msg>a{float:right;

  15.         padding:5px 10px;

  16.         border:1px solid black;

  17.         cursor:pointer;

  18. }  

  19.   </style>

  20.   

  21. </head>

  22. <body>

  23.   <div id="msg">

  24.         <a>X</a>

  25.   </div>

  26.   <script>

  27.     var adv={

  28.         duration:1000,//总时长3秒

  29.         interval:30, //每步间隔50毫秒

  30.         div:null, //保存移动的div对象

  31.         height:0, //div的总高度

  32.         init:function(){

  33.                 //找到div对象,保存在div中

  34.                 this.div=document.getElementById("msg");

  35.                 /*获得div对象高度,保存在height中*/

  36.                 var style=getComputedStyle(this.div);

  37.                         //获得div对象最终应用的所有样式属性

  38.                 this.height=parseFloat(style.height);

  39.         },

  40.         moveUpStep:function(){//向上移动一步

  41.                 var self=this;//用局部变量self转接this指的当前对象

  42.                 //获得div现在最终应用的bottom值

  43.                 var style=getComputedStyle(self.div);

  44.                 var bottom=parseFloat(style.bottom);

  45.                 //将bottom值+=height*interval/duration;

  46.                 bottom+=self.height*self.interval/self.duration;

  47.                 //将bottom的值设置回div的内联样式中,替代外部样式

  48.                 self.div.style.bottom=bottom+"px";

  49.                 //如果bottom值<=0,就再次注册一次性定时器

  50.                 if(bottom<=0){//必须使用adv对象调用moveUpStep

  51.      setTimeout(function(){self.moveUpStep()},self.interval);

  52.                 }

  53.         },

  54.         startMoveUp:function(){//让div开始向上移动

  55.                 var self=this;

  56.                 //注册一次性定时器,放入moveUpStep

  57. setTimeout(function(){self.moveUpStep()},self.interval);

  58.         },

  59.         moveDownStep:function(){//向下移动一步

  60.                 var self=this;

  61.                 //获得div现在最终应用的bottom值

  62.                 var style=getComputedStyle(self.div);

  63.                 var bottom=parseFloat(style.bottom);

  64.                 //将bottom值-=height*interval/duration;

  65.                 bottom-=self.height*self.interval/self.duration;

  66.                 //将bottom的值设置回div的内联样式中,替代外部样式

  67.                 self.div.style.bottom=bottom+"px";

  68.                 //如果bottom值>=-height,

  69.                 if(bottom>=-self.height){

  70.                 //        就再次注册一次性定时器

  71.                         setTimeout(function(){

  72.                                 self.moveDownStep();

  73.                         },self.interval);

  74.                 }else{//否则

  75.                 //  在注册一次startMoveUp,设置间隔为5000毫秒

  76.                         setTimeout(function(){

  77.                                 self.moveUpStep();

  78.                         },5000);

  79.                 }

  80.         },

  81.         startMoveDown:function(){

  82.                 var self=this;

  83.                 //注册一次性定时器

  84.                 setTimeout(function(){

  85.                         self.moveDownStep();

  86.                 },self.interval);

  87.         }

  88. }

  89. window.onload=function(){

  90.         adv.init();

  91.         adv.startMoveUp();

  92.         document.querySelector("#msg>a").onclick=function(){

  93.                 adv.startMoveDown();

  94.         }

  95. }

  96.   </script>

  97. </body>

  98. </html>
复制代码
下面是页面效果:

195758mrvql3a72p72aat5.png

点击右上角的 X ,广告框会慢慢退回去,3秒后广告框又会弹出,如此反复循环。


195758mrvql3a72p72aat5.png
回复

使用道具 举报


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版| 赣南网

© 2013-2016 Comsenz Inc. Powered by Discuz! X3.4

用微信扫一扫

赣南网