timer problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • genesistr
    New Member
    • Aug 2009
    • 13

    timer problem

    Hi,

    I have timer class and some classes use timer.
    but when i come back from timer with callback function, i can see this. properties.

    Code:
    function Timer(dName, dCallback){
        this.timer = null;
    	this.name = dName;
    	this.isRunning = false;
    	this.interval = 50;
    	this.callback = dCallback;
    }
    
    Timer.prototype.Start = function()
    {
    	this.timer = self.setTimeout(this.callback,this.interval);
    	this.isRunning = true;
    }
    Code:
    var timer;
    function File(path) 
    {
    	....
    	if (timer == null) 
    		timer = new Timer("load",File.prototype.WaitForUnFinishedFiles);
    	....
    }
    
    File.prototype.WaitForUnFinishedFiles = function()
    {
    	var isAllFinished = true;
        for(eFile in cache)
        {
            if(!cache[eFile].isReady)
    		{
    			isAllFinished = false;
    			break;
    		}
        }
    	debugtext("load :::" + this.fileName);
    	if(!isAllFinished)
    		timer.Start();
    }
    and i call it in main.js

    Code:
    	
            file = new File("http://bytes.com/images/simon_00.png");
    	file.WaitForUnFinishedF();
    i used debugtext just for testing something but it says undefined. and its important for me to see this. properties after coming back from timer.

    thanks
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    Originally posted by genesistr
    Code:
    file.WaitForUnFinishedF();
    this method is indeed not defined. try file.WaitForUnF inishedFiles();.

    Originally posted by genesistr
    Code:
    timer = new Timer("load",File.prototype.WaitForUnFinishedFiles);
    Code:
    timer = new Timer("load",File.WaitForUnFinishedFiles);
    prototype is not necessary when calling the method…

    Comment

    • genesistr
      New Member
      • Aug 2009
      • 13

      #3
      sorry i pasted with wrong function name. it's already file.WaitForUnF inishedFiles(); .

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        other than that the shown code looks fine.

        Comment

        • genesistr
          New Member
          • Aug 2009
          • 13

          #5
          it looks fine for me also but i dont understand why i lost this' objects. i got this error just when i use timer callback method.

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5390

            #6
            i think you somewhere start your timers ... so the code that is executed is:

            Code:
            Timer.prototype.Start = function() {
                this.timer = self.setTimeout(this.callback, this.interval);
                this.isRunning = true;
            }
            here you loose the scope of this since the timeout 'unbinds' the function from your current scope. you might (probably) fix that the following way (basicly a closure):

            Code:
            Timer.prototype.Start = function() {
                var me = this;
            
                this.timer = self.setTimeout(function() {
                    me.callback.apply(me, arguments);
                }, this.interval);
            
                this.isRunning = true;
            }
            kind regards

            Comment

            • gits
              Recognized Expert Moderator Expert
              • May 2007
              • 5390

              #7
              hi ... since you pmed me that you have further problems with it i had a closer look at it and now i think you basicly want the this of your file-obj closured ... so you would need to use (the previous answer considered to have the this scope of your timer-obj):

              Code:
              function File(path) {
                  this.fileName = path;
                  var me = this;
                  
                  if (timer == null) {
                      timer = new Timer("load", function() {
                          me.WaitForUnFinishedFiles.apply(me, arguments);
                      });
                  }
              }
              kind regards

              Comment

              • gits
                Recognized Expert Moderator Expert
                • May 2007
                • 5390

                #8
                aah ... and perhaps you need to initialize your global timer variable to null?

                Comment

                Working...