2

I have two array: pts and I want to create new test array.

Every time I add new object from pts to test I need to call function that will execute mimicSvg(test,1) function, with array test (from 0 to m)

I write:

var test=[{"X":"300","Y":"400"}];
for(var m=1;m<pts.length;m++){
    var q = pts[m].X;
    var e = pts[m].Y;
    test.push({"X":q,"Y":e});
    setInterval(mimicSvg(test,1), 2000);
  }

but setInterval dont work, instead that I get execution on all m times function at the same time.

How I can solve that problem?

dert detg
  • 303
  • 2
  • 7
  • 18

1 Answers1

2

setInterval takes either a reference to a function to run, or an anonymous function. Because you are trying to call another function with parameters you need to wrap that in an anonymous function declaration. Try this:

setInterval(function() {
    mimicSvg(test,1)
}, 2000);
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • 1
    As a side note, you could still pass the params to setInterval() method, no needs of the anonymous function: `setInterval(mimicSvg, 2000, test, 1);` – A. Wolff Jan 24 '15 at 15:11
  • @A.Wolff I actually didn't realise that signature was included. Every day is a school day :D – Rory McCrossan Jan 24 '15 at 15:11
  • I try my code and work good with setInteral but why this give me all 25 elements at same time, why dont go by step by for loop: var test=[{"X":"300","Y":"400"}]; for(var m=1;m – dert detg Jan 24 '15 at 15:19