A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: setInterval, clearInterval problems

  1. #1
    Restarting from Scratch
    Join Date
    Feb 2001
    Location
    Sacramento, CA
    Posts
    236

    [RESOLVED] setInterval, clearInterval problems

    Hi folks,

    I seem to be creating a loop with setInterval, even though I am using clearInterval. The deck MC and btnQuit MC appear repetitively. I see this same problem quite a few times in the forums but cannot find a solution. Here is the code:

    Code:
    function processInitial() {
    	trace("Begin processInitial()");
    	for (i=0; i<=87; i++) {
    		_root.allCardIDs[i] = _root.initialVars["c_id"+(i+1)];
    	}
    	for (i=0; i<=87; i++) {
    		_root.allCardNames[i] = _root.initialVars["c_name"+(i+1)];
    	}
    	_root.deckOrder = Math.round(Math.random()*88);
    	// Tells the deck the first cardName to display
    	trace("Random number is: "+_root.deckOrder);
    	intervalID = setInterval(displayDeck, 3000);
    	trace("Finish processInitial()");
    }
    
    function displayDeck() {
    	trace("Begin displayDeck()");
    	clearInterval(intervalID);
    	_root.attachMovie("deck", "deck", 50);
    	// 50 refers to the level 
    	_root.deck._alpha = 0;
    	_root.deck.cardName._alpha = 0;	
    	_root.deck._x = 19;
    	_root.deck._y = 174;
    	_root.deck.customFade(100, 20);
    	_root.deck.cardID = _root.allCardIDs[_root.deckOrder];
    	_root.deck.cardName = _root.allCardNames[_root.deckOrder];
    	intervalID = setInterval(displayQuitButton, 2000);	
    	trace("End displayDeck()");
    }
    
    function displayQuitButton() {
    	trace("Begin displayQuitButton()");
    	clearInterval(intervalID);
    	_root.attachMovie("default_button", "btnQuit", 51);
    	// Create the QUIT button
    	_root.btnQuit._alpha = 0;
    	_root.btnQuit._x = 22;
    	_root.btnQuit._y = 463;
    	_root.btnQuit.customFade(100, 10);
    	_root.btnQuit.buttonText = "QUIT";
    	trace("End displayQuitButton()");
    }
    Last edited by Liquid Genius; 09-10-2005 at 09:09 PM.
    Thanks,
    John Z.

  2. #2
    Restarting from Scratch
    Join Date
    Feb 2001
    Location
    Sacramento, CA
    Posts
    236
    This version also creates an infinite loop, and clearInterval fails.
    Code:
    function timer(a, b) {
    	var intervalID = setInterval(a,b);
    }
    
    function processInitial() {
    	trace("Begin processInitial()");
    	for (i=0; i<=87; i++) {
    		_root.allCardIDs[i] = _root.initialVars["c_id"+(i+1)];
    	}
    	for (i=0; i<=87; i++) {
    		_root.allCardNames[i] = _root.initialVars["c_name"+(i+1)];
    	}
    	_root.deckOrder = Math.round(Math.random()*88);
    	// Tells the deck the first cardName to display
    	trace("Random number is: "+_root.deckOrder);
    	timer(displayDeck, 3000);
    	trace("Finish processInitial()");
    }
    
    function displayDeck() {
    	trace("Begin displayDeck()");
    	_root.attachMovie("deck", "deck", 50);
    	// 50 refers to the level 
    	_root.deck._alpha = 0;
    	_root.deck._x = 19;
    	_root.deck._y = 174;
    	_root.deck.customFade(100, 20);
    	_root.deck.cardID = _root.allCardIDs[_root.deckOrder];
    	_root.deck.cardName = _root.allCardNames[_root.deckOrder];
    	clearInterval( intervalID );
    	trace("End displayDeck()");
    }
    My output log reads as follows:
    Begin initialize()
    Sending and Loading
    End initialize()
    Initial variables loaded
    Begin processInitial()
    Random number is: 50
    Begin timer()
    End timer()
    Finish processInitial()
    Load status was: 1
    Begin displayDeck()
    clearInterval should have happened
    End displayDeck()
    Begin displayDeck()
    clearInterval should have happened
    End displayDeck()
    Begin displayDeck()
    clearInterval should have happened
    End displayDeck()
    Wash and repeat, etc, etc, etc, ad-nausium
    I have also tried the original code using different variables for the intervalIDs. (ex: intervalID1, intervalID2) with the same results. Any help would be greatly appreciated!
    Last edited by Liquid Genius; 09-10-2005 at 08:38 PM.
    Thanks,
    John Z.

  3. #3
    Restarting from Scratch
    Join Date
    Feb 2001
    Location
    Sacramento, CA
    Posts
    236
    Apparently, the scope of my clearInterval was incorrect I made sure that it was targeted to _root and everything works now! Answered my own question!

    Code:
    function processInitial() {
    	trace("Begin processInitial()");
    	for (i=0; i<=87; i++) {
    		_root.allCardIDs[i] = _root.initialVars["c_id"+(i+1)];
    	}
    	for (i=0; i<=87; i++) {
    		_root.allCardNames[i] = _root.initialVars["c_name"+(i+1)];
    	}
    	_root.deckOrder = Math.round(Math.random()*88);
    	// Tells the deck the first cardName to display
    	trace("Random number is: "+_root.deckOrder);
    	_root.intervalID = setInterval(displayDeck, 3000);	
    	trace("Finish processInitial()");
    }
    
    function displayDeck() {
    	trace("Begin displayDeck()");
    	_root.attachMovie("deck", "deck", 50);
    	// 50 refers to the level 
    	_root.deck._alpha = 0;
    	_root.deck._x = 19;
    	_root.deck._y = 174;
    	_root.deck.customFade(100, 20);
    	_root.deck.cardID = _root.allCardIDs[_root.deckOrder];
    	_root.deck.cardName = _root.allCardNames[_root.deckOrder];
    	clearInterval( _root.intervalID );
    	_root.intervalID = setInterval(displayQuitButton, 2000);
    	trace("End displayDeck()");
    }
    
    function displayQuitButton() {
    	trace("Begin displayQuitButton()");
    	_root.attachMovie("default_button", "btnQuit", 51);
    	// Create the QUIT button
    	_root.btnQuit._alpha = 0;
    	_root.btnQuit._x = 22;
    	_root.btnQuit._y = 463;
    	_root.btnQuit.customFade(100, 10);
    	_root.btnQuit.buttonText = "QUIT";
    	clearInterval( _root.intervalID );
    	_root.intervalID = setInterval(displayRefineButton, 2000);	
    	trace("End displayQuitButton()");
    }
    
    function displayRefineButton() {
    	trace("Begin displayRefineButton()");	
    	_root.attachMovie("default_button", "btnRefine", 52);
    	// Create the REFINE button	
    	_root.btnRefine._alpha = 0;
    	_root.btnRefine._x = 120;
    	_root.btnRefine._y = 463;
    	_root.btnRefine.customFade(25, 10);
    	_root.btnRefine.buttonText = "REFINE";
    	clearInterval( _root.intervalID );	
    	trace("End displayRefineButton()");
    }
    Thanks,
    John Z.

  4. #4
    Restarting from Scratch
    Join Date
    Feb 2001
    Location
    Sacramento, CA
    Posts
    236

    [resolved]

    [resolved]

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center