0

I want to call myfun1 only once with setInterval. I want to avoid using a global variable. Read this but it does not work (just calls the function every 2000 ms). Naturally I need to call main() every 2000 ms.

(function($){         
    setinterval(main,2000);  

     function main (){            
        if(/*condition*/) return;

        function callItOnce(fn) {
            var called = false;
            return function() {
                if (!called) {
                    called = true;
                    return fn();
                }
                return;
            }
        }

        myfun1 = callITOnce(myfun1);
        myfun1();

        function myfun1(){/*code*/};
        function myfun2(){/*code*/};
        function myfun3(){/*code*/};
})(jquery);
Community
  • 1
  • 1
jacktrades
  • 7,224
  • 13
  • 56
  • 83

4 Answers4

5

Use a flag :

(function($){ 
    var timer = setInterval(main,2000), ran=true;

    function main() {
        if(/*condition*/) return;

        if (ran) { //runs when ran=true, which is only the first time
            myfun1();
            ran = false;  //since it's set to false here
        }

        function myfun1(){/*code*/};
        function myfun2(){/*code*/};
        function myfun3(){/*code*/};

})(jquery);​
adeneo
  • 312,895
  • 29
  • 395
  • 388
  • it looks like OP is trying to perform more than one function within the `main` function. I had the same initial knee-jerk reaction, but I don't think that this is necessarily the correct answer. – zzzzBov Aug 02 '12 at 19:47
  • @adeno: OP wants to execute everything else inside `main` all the time, except for the call to `myfun1` which OP only wants to execute once. – Nope Aug 02 '12 at 19:49
  • Correct! Thanks for understanding – jacktrades Aug 02 '12 at 19:51
  • 1
    +1. I just fixed the syntax errors in the sample code so it can execute. See working DEMO here: http://jsfiddle.net/FranWahl/aWWay/ – Nope Aug 02 '12 at 19:56
0

The callItOnce function you wrote should work. You just need to declare the function outside the setInterval call. Otherwise it gets redefined each time.

(function () {

function myfun1(){/*code*/};
function myfun2(){/*code*/};
function myfun3(){/*code*/};

function callItOnce(fn) {
    var called = false;
    return function() {
        if (!called) {
            called = true;
            return fn();
        }
        return;
    }
}

myfun1 = callItOnce(myfun1);

function main() {
    if(/*condition*/) return;
    myfun1();
}

setInterval(main, 2000);

}());
juandopazo
  • 6,283
  • 2
  • 26
  • 29
0

One easy way to work around this is to use a variable on the function itself.

function myFunc1(){

if(arguments.callee.done){return;}


arguments.callee.done = true;    

}

This way the "done" variable on the method myFunc1 would make sure the method is executed only once.

Segu
  • 266
  • 2
  • 2
-1

setInterval returns a value that you can pass to clearInterval to stop the callback from being called.

var i = 0;
var timeout_id = setTimeout(function(){
    console.log(i);
    if(i >= 5){ clearTimeout(timeout_id) }
    i++;
})
hugomg
  • 68,213
  • 24
  • 160
  • 246
  • That's another option, but I want to call only myFun1() once, others with the interval... – jacktrades Aug 02 '12 at 19:49
  • 1
    @missingno: This will stop the callback altogether. OP seems to want the callback to continue but not execute `func1` inside it more than once. – Nope Aug 02 '12 at 19:51