-1

I want to create a function that begins an interval loop. The interval loop invokes another function.

So something like this:

function myFunction(param1,param2) {
    setInterval(function() {
        myFunction2(param1,param2);
  }, 1000);
}

function myFunction2(param1, param2) {
    //do something.
}

But from what I have found so far, setInterval needs to take an annoymous function and obviously param1 and param2 will be undefined.

How could this be acheived?

  • please explain that, as far as i understand your question, your code does exactly what you describe. – Thomas Jan 18 '16 at 15:39
  • 1
    http://stackoverflow.com/questions/457826/pass-parameters-in-setinterval-function – Eli Stone Jan 18 '16 at 15:40
  • and JS-Code can't tell apart an anonymous function and a non-anonymous; not reliable, not if you think in terms of minifyers and so. – Thomas Jan 18 '16 at 15:41
  • Your code, exactly as you have it, works just fine. `param1` and `param2` will be as you want them. This is called a Javascript closure and is a supported feature of the language. – jfriend00 Jan 18 '16 at 15:47

1 Answers1

0

This is working just fine:

function myFunction(param1,param2) {
    setInterval(function() {
        myFunction2(param1,param2);
  }, 1000);
}

function myFunction2(param1, param2) {
    console.log(param1);
}

myFunction('test', 1);