javascripttypescriptfunctionspine

How to disable function when another function is called?


I wish to disable the opacity fade out function when stop win function is running. I'm using Typescript. How can I do that? Your kindness help is appreciated.

Method that I tried:

  1. Add if statement that include setTimeout function, but it show stopBigWinAnimation type is void

  2. Add if statement that include setTimeout function, declare Boolean type for stopBigWinAnimation and add a return, however Uncaught RangeError: Maximum call stack size exceeded

  // Stop Win function
  public stopBigWinAnimations() {

      this.mainContainer.opacity = 255

      let animBigWin4 =  this.nodes4.getComponent(sp.Skeleton);
      // Maybe it is not animated or it is the 0th empty node
      if (animBigWin4) {
        animBigWin4.clearTracks();
        animBigWin4.setToSetupPose(); 
        if (animBigWin4.defaultAnimation) {
          animBigWin4.setAnimation(0, animBigWin4.defaultAnimation, true);
        }
      }
    }

    // Fade Out function
    setTimeout(function () {
       this.nodes5.opacity = 0;
       this.nodes6.opacity = 0;
       this.nodes7.opacity = 0;
       this.nodes8.opacity = 0;
    }.bind(this), 6500);

Expect Result: While Stop Win Function, Fade out function is being disable.


Solution

  • If I understand correctly, you want to disable the setTimeout when stopBigWinAnimations is called.

    To do that, you need to name the setTimeout (fadeOutFunction) so you can "clear" it whenever your stopBigWinAnimations function runs.

    let fadeOutFunction = null;
    
    public stopBigWinAnimations() {
          clearTimeout(fadeOutFunction);
    
          this.mainContainer.opacity = 255
    
          let animBigWin4 =  this.nodes4.getComponent(sp.Skeleton);
          // Maybe it is not animated or it is the 0th empty node
          if (animBigWin4) {
            animBigWin4.clearTracks();
            animBigWin4.setToSetupPose(); 
            if (animBigWin4.defaultAnimation) {
              animBigWin4.setAnimation(0, animBigWin4.defaultAnimation, true);
            }
          }
        }
    
        // Fade Out function
        fadeOutFunction = setTimeout(function () {
           this.nodes5.opacity = 0;
           this.nodes6.opacity = 0;
           this.nodes7.opacity = 0;
           this.nodes8.opacity = 0;
        }.bind(this), 6500);