A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Clear Interval Within Function

  1. #1
    Junior Member
    Join Date
    Jan 2006
    Posts
    24

    Clear Interval Within Function

    Hello,
    I'm working in Flash V8.
    I want to clear an Interval within a function but I'm not sure how. setInterval is cleared fine when not within the function, but I want to call it from a MCbutton. Could someone please show me how to do such a thing. Thank You, Jacob


    //For Loop to Animate Text
    function setAnimate():Void {
    //Animated Property Changes
    containerMC1._alpha = nChange++
    containerMC1._xscale = nChange++
    containerMC1._yscale = nChange++
    //Counter
    nTimes++
    //Clears Interval
    if(nTimes >= 35) {
    clearInterval(nInterval);
    }
    }

    var nChange:Number = 0;
    var nTimes:Number = 0;

    //Problem Area....!!!
    //Start Interval Function
    function startInterval():Void {
    var nInterval:Number = setInterval(setAnimate, 8);
    }

  2. #2
    Qwaizang:syntax_entity_ Q__Hybrid's Avatar
    Join Date
    Aug 2005
    Posts
    270
    The reason why your interval is not being cleared is because the scope of the variable "nInterval" is localized within startInterval().
    To fix this, write your code as follows:
    Code:
    var nChange:Number = 0;
    var nTimes:Number = 0; 
    var nInterval:Number = undefined;
    
    //For Loop to Animate Text
    function setAnimate():Void {
    //Animated Property Changes
        containerMC1._alpha = nChange++;
        containerMC1._xscale = nChange++;
        containerMC1._yscale = nChange++;
    //Counter
        nTimes++;
    //Clears Interval
        if(nTimes >= 35) {
            clearInterval(nInterval);
        }
    }
    
    //Start Interval Function
    function startInterval():Void {
        nInterval = setInterval(setAnimate, 8);
    }
    This will make the scope of nInterval more broad so that more than one function can access it.
    +Q__

  3. #3
    Junior Member
    Join Date
    Jan 2006
    Posts
    24
    Brilliant

    Thanks, I would have not figured that one out.

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