3

my question is how do I call the function "changeArray" after one second? I want the first "changeArray" to be called after 1 second, the next after 2 seconds and so on. So every function should be called 1 second after the previous one has been executed.

var array = [
    [0,0,0],
    [0,0,0],
    [0,0,0]
];

function changeArray(i,j) {
    array[i][j] +=1;
}

changeArray(1,1); //after 1 second
changeArray(1,1); //after 2 seconds
changeArray(0,0); //after 3 seconds
changeArray(0,0); //after 4 seconds

console.log(array);
bady
  • 99
  • 8
  • http://stackoverflow.com/questions/729921/settimeout-or-setinterval – Paul Abbott May 03 '17 at 21:25
  • Can you elaborate on the "with different parameters" part? – Rob M. May 03 '17 at 21:31
  • when I call the changeArray(1,1) function with the parameters "1,1" and in the next step by calling changeArray(0,0) function with the parameters "0,0" or "arguments", maybe that's how you may call them. All I want is that, when I call changeArray 4 times, that it should be called always after 1 second and that the array should be modified every second in 4 steps until 4 seconds are over and then I want the result. – bady May 03 '17 at 21:45

4 Answers4

1

If there is no rule on how you decide which parameters to pass to changeArray, you can try this:

function addInterval(func, i) {
    window.setTimeout(function() {
        func();
    }, 1000 * i);
}

addInterval(() => changeArray(1, 1), 1);
addInterval(() => changeArray(1, 1), 2);
addInterval(() => changeArray(0, 0), 3);
addInterval(() => changeArray(0, 0), 4);
Malte Hartwig
  • 4,477
  • 2
  • 14
  • 30
  • It shows an error, when I try to use your example with mine "TypeError: functions[i] is not a function. (In 'functions[i]()', 'functions[i]' is undefined)". – bady May 03 '17 at 22:24
  • @bady I changed the code to make it work. It was the issue with closures that other commenters refered to – Malte Hartwig May 03 '17 at 22:36
  • thanks, it's close to that what I need, it works almost like I wanted it, however it would be better if it would be a bit more dynamic. I mean in this case whenever I want to add another function, I have to pass it to the scheduler ... is there maybe dynamic way for example to have the like schedule(changeArray(1,1),**1**) and the last one stands for the second in which the function will be executed? and the next one like changeArray(1,1),**2**) and after 2 secs? – bady May 03 '17 at 22:56
  • I have changed it, making it easier to understand and more flexible. The addInterval method is now just a helper method to "hide" the setTimeout method. – Malte Hartwig May 03 '17 at 23:09
  • 1
    I just would like to add that arrow functions are ES6 feature, so don't expect that to work with "old browsers" unless you have something like babel in place. – Gabriel Oliveira May 04 '17 at 13:25
1

You can use the Window setTimeout method. If you need a better syntax or use it inside a loop you can define the helper method bellow:

defer = function(method, seconds, args) {
  var fn = function() {
      return method.apply(null, args);
  }

  return setTimeout(fn, seconds * 1000);
}

And use it like that:

defer(changeArray, 1, [1, 1]);
defer(changeArray, 2, [1, 1]);
defer(changeArray, 3, [0, 0]);
defer(changeArray, 4, [0, 0]);

Or in a for loop:

var count = 1;
for(var i = 0; i < array.length; i++) {
    var row = array[i];
    for(var j = 0; j < row.length; j++) {
        defer(changeArray, count, i, j);
        count++;
    }
}
Gabriel Oliveira
  • 790
  • 8
  • 19
  • I have tried it, I don't know why but all function were executed at the first second ... And can't I use it like: defer(_changeArray(1, 1), 1)_; ? – bady May 03 '17 at 22:58
  • No, because `changeArray(1, 1)` is executing the function immediately and passing the result as first argument to defer. I tested again and seens to work. The second argument to defer should be the desired "timeout". – Gabriel Oliveira May 03 '17 at 23:10
0

Check out setTimeout and setInterval:

setTimeout(() => changeArray(1,1), 1000); //after 1 second
lxe
  • 7,051
  • 2
  • 20
  • 32
  • I have already checked it, but the problem is that I want the the changeArray function to be modified, so that it is called for the first time after one second and then every time changeArray is called, It should be called 1 second after the previous changeArray function has been executed. In the code is also an example, where I have commented when I want which function to be called. – bady May 03 '17 at 21:38
0
var counter=0
setInterval(function(){
++window.counter;
var a, b;
if (window.counter== /*however many seconds */ ) {
a=//what ever you want for the time passed
b=//what ever you want for the time passed
}
// add more if statements like that for different times
changeArray(a, b);
 }, 1000)
user7951676
  • 377
  • 2
  • 10