0

when user click element it should initiate the timeout and set the starttime to true. Then if user clicks element again while the timer is active it should clear the timeout and reinitiate the timeout. However im still getting the console from the first and the following timeout initiated. Shouldnt the cleartimeout leave me with only one, the final one created? Ive already tried the example from this question here How to reset timeout in Javascript/jQuery?.

    toggleCard: function(card){
                var Timer = null;
                if(this.startTime){
                    console.log('already activated');
                    window.clearTimeout(Timer);
                    this.startTime = false;
                }

                this.startTime = true;
                var Timer = setTimeout(function(){
//                        this.taskCard = false;
//                        this.classCard = true;
                    console.log('old timer')
                }.bind(this), 5000);
0

4 Answers 4

1

define global variable e.g.

var myTimeOut = 0;

then inside your method call clearTimeout

toggleCard: function(card){
                clearTimeout(myTimeOut);
                this.startTime = true;
                myTimeOut = setTimeout(function(){
//                        this.taskCard = false;
//                        this.classCard = true;
                    console.log('old timer')
                }.bind(this), 5000);
Sign up to request clarification or add additional context in comments.

Comments

1

Do like this:

toggleCard: function(card) {
    window.Timer = null; // <-----------declare timer globally here
    if (this.startTime) {
      console.log('already activated');
      window.clearTimeout(Timer); // <------- clear here if it is.
      this.startTime = false;
    }

    Timer = setTimeout(function() { // <-------assign the setTimeout here
      this.startTime = true; // <---------It has to be here.
      //   this.taskCard = false;
      //   this.classCard = true;
      console.log('old timer')
    }.bind(this), 5000);

Comments

1

I think this code should meet your requirement.

 var timer = null;
    var started = false;
    toggleCard = function (card) {
        if (started) {
            console.log('Timer already active');
            window.clearTimeout(timer);
        } else {
            console.log('Timer active now');
            started = true;
        }

        timer = setTimeout(function () {
            console.log('Timer elapsed! ');
            started = false;
        }.bind(this), 2000);
    };

Comments

0

Every time the function is called you are creating a new Timer variable, so when you try to clear the timeout, the Timer var doesn't reference the actual timeout you created.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.