Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    All variables are undefined in function called by Timer

    Hello everyone I'm Neil. I've been learning javascript for a few weeks now. I've found myself really stuck on something. I'm using the Timer. My problem is in the function that the Timer calls at each interval. For some reason all of the variables read undefined from that function. Oddly, with the exception of my screen variable.

    This is more code than I probably should post, but I'm really not sure where the problem is. The startSequence method starts the timer and passes the flashSquare function. I've put an alert box in the flashSquare that displays if these variables are undefined and the alert does show.

    Any help will be greatly appreciated.

    Code:
    MemoryGame = function() {
    	this.screen = new Screen();
    	this.score = new Score();
    	this.colors = new Array();
    	this.colorIndex = 0;
    	this.timerId;
    	this.showColor = true;
    	this.showingSequence = false;
    	
    	MemoryGame.prototype.init = function() {
    		this.screen.drawTitle();
    	}
    	
    	MemoryGame.prototype.startGame = function() {
    	    clearInterval(this.timerId);
    		this.colors = new Array;
    		this.score.reset();
    		this.startSequence();
    	}
    	
    	MemoryGame.prototype.startSequence = function() {
    		this.timerId = setInterval(this.flashSquare, 500);
    		this.showColor = true; 
    		this.showingSequence = true;
    		this.colorIndex = 0;
    	}
    	
    	MemoryGame.prototype.flashSquare = function() {
       
    		if (this.showColor == undefined && this.showingSquence == undefined
    			    && this.colorIndex == undefined && this.colors == undefined) {
            	
    		alert("These are all undefined");
            	clearInterval(this.timerId); // this doesn't stop the timer though because it's undefined!
            }
    	}
    }

  • #2
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok no need for anyone to reply. I've figured it now. I googled setInterval and scope and it seems I'm not the first to have this problem!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •