时间轴对象构造器

在用javascript做自动播放的动画时,需要在不同时刻启动多个定时器,这时可以构造一个时间轴。

var scence = new TimeLine() //声明

scence.add(time,func..,'init')  //添加动作

scene start()//执行动作

一个简单的小例子

 1 var TimeLine = function(){
 2     this.order = []; //动画的序列
 3     this.add = function(timeout, func, log){
 4         this.order.push({
 5             timeout:timeout,
 6             func:func,
 7             log:log,
 8         });
 9     }
10     this.start = function(ff){
11         for(s in this.order){
12             (function(me){
13                 var fn = me.function,
14                 var timeout = me.timeout,
15                 var log = me.log;
16                 timeout = Math.max(time-(ff||0),0)
17 
18                 setTimeout(fn,timeout);
19                 setTimeout(function(){
20                     console.log('time->',timeout,'log->',log)
21                 },timeout);
22             })(this.order[s])
23         }
24     }
25 }
26 s.add(100,function(){
27     console.log('first')
28 },'int')
29 
30 s.add(1500,function(){
31     console.log('sec')
32 },'int')
33 s.start();

具体例子可参考https://github.com/daisychuckle1006/Dragon-Boat-Festival-animation

原文地址:https://www.cnblogs.com/daisy-ramble/p/5585276.html