-1

I'm trying to disable 2 functions when a certain time period is reached and enable the other 2 after that time period. So the second 2 functions would have to be disabled to begin with.

I was thinking of using the following code to wrap around the functions:

Code:

var startTime = new Date().getTime();
var interval = setInterval(function(){
if(new Date().getTime() - startTime > 5000){
    clearInterval(interval);
    return;
}

function 1() {}
$(function 2() {});

}, 1000);

function 3() {}
$(function 4() {});

Can you help?

3
  • 1
    when you say "turn off" the first functions. Are you going to have the first functions firing on an interval? Then you want to cancel that interval, and then start a new one w/ the new functions?
    – Brodie
    Commented Jul 9, 2015 at 13:27
  • 4
    Disabling and enabling functions is a very obscure explanation. Disable in what way? Remove them? Replace them? Perhaps if you explain the overall aim. Commented Jul 9, 2015 at 13:28
  • This is why i thought the setInterval was my best bet for this Commented Jul 9, 2015 at 14:25

4 Answers 4

2

If you want to control whether functions do something or not, based on how much time has elapsed, it would probably be easier to set a flag after the interval you need, and then have your functions check that flag to decide if they are going to do something:

var timedOut = false;

setTimeout(function () {
    timedOut = true;
}, 5000);

function one() {

    if (!timedOut) {
        // do something
    }

}

function two() {

    if (!timedOut) {
        // do something
    }

}

function three() {

    if (timedOut) {
        // do something
    }

}

function four() {

    if (timedOut) {
        // do something
    }

}
6
  • I do like this approach, but I dislike the fact that the flag now becomes global scope. This may not be a big deal to the OP, but I like to keep things as minimalistic as possible. (Arguably, defining my two functions in global scope is no better though, so :shrug: (though I could have just as easily modified an existing object)) Commented Jul 9, 2015 at 13:41
  • your fiddle doesn't have any html - how can you tell if it's working or not? Commented Jul 9, 2015 at 14:36
  • @Starscream1984, I've attached it to my site and for some reason the function isn't working. instead of doing function 3 and 4, it closes the page. it does function 1 and 2 within the time though. Commented Jul 9, 2015 at 14:39
  • What calls the functions? Commented Jul 9, 2015 at 14:43
  • It's tricky to guess then at exactly why the '3' and '4' functions don't fire, without being able to debug the code in action... do breakpoints in those functions get hit at all? Commented Jul 9, 2015 at 15:14
1

This should get you started; I've simply redefined the original func1/func2 functions after a set time (5 seconds, as your example uses). This could do any number of things (such as remove the function definition altogether).

(function(document,window,undefined){
    // Used simply to show output to the window.
    var db = document.getElementById('db');
   
    // Here we define the initial state of our two functions.
    // Nothing magical here, just outputting a description.
    window.func1 = function(){
        db.innerHTML += 'Hello from original func1\r\n';
    }
    window.func2 = function(){
        db.innerHTML += 'Hello from original func2\r\n';
    }
    
    // Here we keep the same format you used (using the Date to
    // define when one's been deprecated over the other).
    var startTime = new Date().getTime(),
        interval = setInterval(function(){
            var currentTime = new Date().getTime(),
                delta = currentTime - startTime;
            if (delta > 5000){
              
                // In here, now that the specified amount of time has
                // elapsed, we redefine the meaning of the two original
                // functions. We could also simply remove them.
                window.func1 = function(){
                    db.innerHTML += 'Hello from NEW func1\r\n';
                }
                window.func2 = function(){
                    db.innerHTML += 'Hello from NEW func2\r\n';
                }
                
                clearInterval(interval);
            }
        }, 1000);
})(document,window);

// This is here just to show you how one definition is changed
// in place of another.
setInterval(function(){
    func1();
    func2();
}, 1000);
<pre id="db"></pre>

0

If you mean 'disabling' the functions after certain amount of seconds then this should do the trick.

var secondsLimit = 10,
    a = 0,
    b = setInterval(function () { a += 1; }, 1000 });

function A() {
   if (a > secondsLimit) {
      return;
   }

   // do stuff
}
0

You can change the functions if you call them e.g. by a global variable scope. In the following example based on your code, the functions switch after 4 seconds.

var function1 = function() {
    console.log("function 1 active");
};

var function2 = function() {
    console.log("function 2 active")
}

var startTime = new Date().getTime();

setTimeout(function() {
    function1 = function() {
        console.log("now function 3 is active instead of function 1");
    }

    function2 = function() {
        console.log("now function 4 is active instead of function 2");
    }
}, 4000);

//the following code is just for testing reasons
var interval = setInterval(function() {
    function1();
    function2();
}, 1000)

Not the answer you're looking for? Browse other questions tagged or ask your own question.