Dan W
Dan W

Reputation: 365

Javascript timeout after conditions is met

I have a condition that once met I want to timeout that function so it is not called again for X amount of time. After that time period it can be called once again if the condition is met. how can I achieve this?

    setTimeout(function captureTimeout() {
        console.log("CheckingScore");
        if (ctrack.getScore() >= 0.8){
        console.log("Capturing Data") ;

        setTimeout(captureTimeout(), 30000);

    }
}, 1000);

captureTimeout();

Fairly certain the above code it extremely wrong!

@JLRishe

I am getting the score of ctrack.getScore() which ranges from 0.0 - 1.0, If the value reaches 0.8 or above I want to execute a function which will capture elements of the page at that moment (Looking for a solution for this) but for now I just want to either give an Alert or Log the console with a message. Once executed I want a "cooldown" period of 30 seconds before that function can be executed again, so even if ctrack.getScore() reaches 0.8 or more I only want the function to execute after the cooldown has finished and if the condition is met again after that cooldown period. Hopefully that explains it a bit more?

Upvotes: 0

Views: 164

Answers (2)

JLRishe
JLRishe

Reputation: 101738

I'm pretty sure what you're trying to accomplish has nothing to do with setTimeout.

Basically what you need to do here is keep track of when the cooldown period is going to be over and either:

  • Use an if statement to skip the code that calls your function until the cooldown period is over OR
  • Put logic in the function to prevent it from doing anything until the cooldown period is over

I like the second option a bit better because it applies no matter who tries to call the function, and here's how you could do this:

var coolDownSeconds = 30,
    nextAllowedCapture = new Date();

function capturePage() {
    var now = new Date();

    if (nextAllowedCapture <= now) {
        // Do capture logic
        console.log("Capturing...");

        nextAllowedCapture = new Date();
        nextAllowedCapture.setSeconds(nextAllowedCapture.getSeconds() + 
                                      coolDownSeconds);
    } else {
        console.log("Can't capture again yet. This function can be executed again in " + 
                    (nextAllowedCapture.getTime() - now.getTime()) / 1000 +
                    " seconds.");
    }
}

http://jsfiddle.net/3U7Ud/2/

Upvotes: 1

skyline3000
skyline3000

Reputation: 7913

You'll want to keep a boolean flag to know if you should perform the logic in your function or not. If your function should perform the logic, then you can set the flag to false and start the timeout for when it will become true again. That way, additional calls to the function won't perform the logic until the timeout has finished. Something like this:

var flag = true;

function myFunction() {
    if (flag === true) {

        // Don't let this run again for 30s
        flag = false;
        window.setTimeout(function() {
            flag = true;
        }, 30000);

        // perform the rest of your function logic...
    }
};

Upvotes: 1

Related Questions