changeset 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 569b89a74983
children ae2faf140dc2
files nodejs/animate.js
diffstat 1 files changed, 12 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/nodejs/animate.js	Wed Sep 01 02:07:15 2010 +0800
+++ b/nodejs/animate.js	Wed Sep 01 16:23:43 2010 +0800
@@ -8,6 +8,7 @@
  */
 var ffs = 20;
 var frame_interval = 1000 / ffs;
+var tm_flag = 1;
 
 function linear_draw() {
     var percent;
@@ -20,11 +21,17 @@
     if(percent >= 1) {
 	this.obj.timer.stop();
 	delete this.obj.timer;
+	sys.puts(Date.now() - this._start_tm);
     }
     
     this.obj[2] = (this.targetx-this.startposx)*percent+this.startposx;
     this.obj[5] = (this.targety-this.startposy)*percent+this.startposy;
     this.app.refresh();
+    if(tm_flag) {
+	var self = this;
+	if(percent < 1)
+	    this.obj.timer = setTimeout(function() { self.draw(); }, frame_interval);
+    }
 }
 
 function linear_draw_start() {
@@ -39,7 +46,11 @@
     this.c = 0;
     this.step = 1000 / (this.duration * ffs);
     this.percent = 0;
-    obj.timer = setInterval(function() { self.draw(); }, frame_interval);
+    this._start_tm = Date.now();
+    if(tm_flag == 1)
+	obj.timer = setTimeout(function() { self.draw(); }, frame_interval);
+    else
+	obj.timer = setInterval(function() { self.draw(); }, frame_interval);
 }
 
 function linear(app,obj,shiftx,shifty,duration) {