comparison nodejs/animate.js @ 804:55875133fa60

Compare performance of setInterval() and setTimeout()
author Thinker K.F. Li <thinker@codemud.net>
date Wed, 01 Sep 2010 16:23:43 +0800
parents 210e4d24a3ba
children ae2faf140dc2
comparison
equal deleted inserted replaced
803:569b89a74983 804:55875133fa60
6 * This is configuration for animate module. For slower or speeder 6 * This is configuration for animate module. For slower or speeder
7 * machines, ffs can be decreased or increased respective. 7 * machines, ffs can be decreased or increased respective.
8 */ 8 */
9 var ffs = 20; 9 var ffs = 20;
10 var frame_interval = 1000 / ffs; 10 var frame_interval = 1000 / ffs;
11 var tm_flag = 1;
11 12
12 function linear_draw() { 13 function linear_draw() {
13 var percent; 14 var percent;
14 15
15 this.percent = this.percent + this.step; 16 this.percent = this.percent + this.step;
18 19
19 this.c++; 20 this.c++;
20 if(percent >= 1) { 21 if(percent >= 1) {
21 this.obj.timer.stop(); 22 this.obj.timer.stop();
22 delete this.obj.timer; 23 delete this.obj.timer;
24 sys.puts(Date.now() - this._start_tm);
23 } 25 }
24 26
25 this.obj[2] = (this.targetx-this.startposx)*percent+this.startposx; 27 this.obj[2] = (this.targetx-this.startposx)*percent+this.startposx;
26 this.obj[5] = (this.targety-this.startposy)*percent+this.startposy; 28 this.obj[5] = (this.targety-this.startposy)*percent+this.startposy;
27 this.app.refresh(); 29 this.app.refresh();
30 if(tm_flag) {
31 var self = this;
32 if(percent < 1)
33 this.obj.timer = setTimeout(function() { self.draw(); }, frame_interval);
34 }
28 } 35 }
29 36
30 function linear_draw_start() { 37 function linear_draw_start() {
31 var obj = this.obj; 38 var obj = this.obj;
32 var self = this; 39 var self = this;
37 this.startposx = obj[2]; 44 this.startposx = obj[2];
38 this.startposy = obj[5]; 45 this.startposy = obj[5];
39 this.c = 0; 46 this.c = 0;
40 this.step = 1000 / (this.duration * ffs); 47 this.step = 1000 / (this.duration * ffs);
41 this.percent = 0; 48 this.percent = 0;
42 obj.timer = setInterval(function() { self.draw(); }, frame_interval); 49 this._start_tm = Date.now();
50 if(tm_flag == 1)
51 obj.timer = setTimeout(function() { self.draw(); }, frame_interval);
52 else
53 obj.timer = setInterval(function() { self.draw(); }, frame_interval);
43 } 54 }
44 55
45 function linear(app,obj,shiftx,shifty,duration) { 56 function linear(app,obj,shiftx,shifty,duration) {
46 obj.animated_linear = this; 57 obj.animated_linear = this;
47 this.app = app; 58 this.app = app;