5 Replies Latest reply on Jul 28, 2006 3:19 AM by Goo101

    setTimeout/setInterval

    Goo101 Level 1
      Hi there,
      I designed the following code to return my application to it's Main screen when the user enters an invalid voucher number (note: they need a valid voucher number in order to be able to proceed with their purchase). I want the app to return to the Main screen after a brief 5 second time delay during which it displays an error message to the user in a dynamic text field stating that their voucher number is invalid.
      Please see the code below . . .

      //Define the MainSlide function . . .
      function MainSlide():Void {
      clearTimeout;
      // GoTo Screen behavior
      if((this._parent.Main != undefined) && (this._parent.Main != null))
      {
      var screen = null;
      var target = this;
      while((screen == null) && (target != undefined) && (target != null))
      {
      if(target instanceof mx.screens.Screen)
      {
      screen = target;
      }
      else
      {
      target = target._parent;
      }
      }
      if(screen instanceof mx.screens.Slide)
      {
      screen.gotoSlide(this._parent.Main);
      }
      // End GoTo Screen behavior
      }
      }

      //Then use it in the if statement logic . . .

      // Checks the validity of VoucherNo based to the last good known Voucher Number
      // and upper Voucher range (based on tomorrow's date)
      if (itxtVoucherNo.length < 14) {
      //changes the dynamic text field to display an "Invalid Voucher Number" message
      this.dtxtVoucherNo.text = "invalid voucher number!";
      //calls the MainSlide function to return the app. to the Main screen after a 5
      //second delay
      setTimeout(MainSlide, 5000);
      // MainSlide();
      }
      else if (VoucherNo > this._parent.Connect_Check.PreviousValidVoucher and VoucherNo < upperVoucher) {
      //if Voucher Number is valid the app. goes to the next screen
      // GoTo Screen behavior
      if((this._parent.Format != undefined) && (this._parent.Format != null))
      {
      var screen = null;
      var target = this;
      while((screen == null) && (target != undefined) && (target != null))
      {
      if(target instanceof mx.screens.Screen)
      {
      screen = target;
      }
      else
      {
      target = target._parent;
      }
      }
      if(screen instanceof mx.screens.Slide)
      {
      screen.gotoSlide(this._parent.Format);
      }
      }
      // End GoTo Screen behavior
      trace("Voucher Number is valid!");
      }
      else {
      //changes the dynamic text field to display an "Invalid Voucher Number" message
      this.dtxtVoucherNo.text = "invalid voucher number!";
      //calls the MainSlide function to return the app. to the Main screen after a 5
      //second delay
      setTimeout(MainSlide, 5000);
      }
      }

      As you can see from the above code, there is a commented out line which just calls the MainSlide() function without the 5 second time delay and this logic works fine but obviously doesn't give me enough time to display my error message in my dynamic text field.
      I believe that I have set this code up correctly but yet it still doesn't work. I must be doing something wrong. Can someone please take a look at this and let me know where I'm going wrong? Please!
      Thanx.
        • 1. Re: setTimeout/setInterval
          Level 7
          I could not figure out what exactly is the problem that you want to fix. Is
          the MainSlide function not getting called after 5 seconds?


          "Goo101" <webforumsuser@macromedia.com> wrote in message
          news:ea7gqm$1h$1@forums.macromedia.com...
          > Hi there,
          > I designed the following code to return my application to it's Main screen
          > when the user enters an invalid voucher number (note: they need a valid
          > voucher
          > number in order to be able to proceed with their purchase). I want the app
          > to
          > return to the Main screen after a brief 5 second time delay during which
          > it
          > displays an error message to the user in a dynamic text field stating that
          > their voucher number is invalid.
          > Please see the code below . . .
          >
          > //Define the MainSlide function . . .
          > function MainSlide():Void {
          > clearTimeout;
          > // GoTo Screen behavior
          > if((this._parent.Main != undefined) && (this._parent.Main != null))
          > {
          > var screen = null;
          > var target = this;
          > while((screen == null) && (target != undefined) && (target !=
          > null))
          > {
          > if(target instanceof mx.screens.Screen)
          > {
          > screen = target;
          > }
          > else
          > {
          > target = target._parent;
          > }
          > }
          > if(screen instanceof mx.screens.Slide)
          > {
          > screen.gotoSlide(this._parent.Main);
          > }
          > // End GoTo Screen behavior
          > }
          > }
          >
          > //Then use it in the if statement logic . . .
          >
          > // Checks the validity of VoucherNo based to the last good known Voucher
          > Number
          > // and upper Voucher range (based on tomorrow's date)
          > if (itxtVoucherNo.length < 14) {
          > //changes the dynamic text field to display an "Invalid Voucher Number"
          > message
          > this.dtxtVoucherNo.text = "invalid voucher number!";
          > //calls the MainSlide function to return the app. to the Main screen
          > after a
          > 5
          > //second delay
          > setTimeout(MainSlide, 5000);
          > // MainSlide();
          > }
          > else if (VoucherNo > this._parent.Connect_Check.PreviousValidVoucher and
          > VoucherNo < upperVoucher) {
          > //if Voucher Number is valid the app. goes to the next screen
          > // GoTo Screen behavior
          > if((this._parent.Format != undefined) && (this._parent.Format !=
          > null))
          > {
          > var screen = null;
          > var target = this;
          > while((screen == null) && (target != undefined) && (target !=
          > null))
          > {
          > if(target instanceof mx.screens.Screen)
          > {
          > screen = target;
          > }
          > else
          > {
          > target = target._parent;
          > }
          > }
          > if(screen instanceof mx.screens.Slide)
          > {
          > screen.gotoSlide(this._parent.Format);
          > }
          > }
          > // End GoTo Screen behavior
          > trace("Voucher Number is valid!");
          > }
          > else {
          > //changes the dynamic text field to display an "Invalid Voucher Number"
          > message
          > this.dtxtVoucherNo.text = "invalid voucher number!";
          > //calls the MainSlide function to return the app. to the Main screen
          > after a
          > 5
          > //second delay
          > setTimeout(MainSlide, 5000);
          > }
          > }
          >
          > As you can see from the above code, there is a commented out line which
          > just
          > calls the MainSlide() function without the 5 second time delay and this
          > logic
          > works fine but obviously doesn't give me enough time to display my error
          > message in my dynamic text field.
          > I believe that I have set this code up correctly but yet it still doesn't
          > work. I must be doing something wrong. Can someone please take a look at
          > this
          > and let me know where I'm going wrong? Please!
          > Thanx.
          >


          • 2. Re: setTimeout/setInterval
            Goo101 Level 1
            Yes, the MainSlide function is not getting called after 5 seconds.

            If I just call the MainSlide function directly (using the commented out line "MainSlide();") the app. works and goes to the Main slide. But this does not allow sufficient time to display the error message ("this.dtxtVoucherNo.text = "invalid voucher number!";") in the dynamic text field.

            But if I try to do it using the setTimeout method it won't work, it does display the error message in the dynamic text field but then the app. just sits there and won't return to the Main slide via the MainSlide() function.

            Why is this happening? I have checked and double checked this and my code looks good to me?
            • 3. Re: setTimeout/setInterval
              Goo101 Level 1
              Can anyone out there help me with the above problem? Please!
              • 4. Re: setTimeout/setInterval
                kglad Adobe Community Professional & MVP
                are you publishing for flash 8?

                p.s. and your clearTimeout needs an interval id to work. and you don't need to use it in MainSlide() unless MainSlide() can be called by something other than your setTimeout() function.
                • 5. Re: setTimeout/setInterval
                  Goo101 Level 1
                  Yes, I'm publishing using Flash 8 . . . why is this a factor? Can you explain?

                  Also, in your p.s. comments, are you saying that I should decalre the setTimeout method as a variable in order to be able to use the clearTimeout method. Something like the following . . .

                  var TimeDelay:Number = setTimeout(MainSlide, 5000);

                  clearTimeout(TimeDelay);

                  Is it necessary to code a setTimeout method in this way?