1

I want a function in pure JS to wait and execute only when a certain condition happens.

function wait_then_go  (a,b,c) {

if (!go) window.setTimeout (wait_then_go (a,b,c), 500);

// go here 

}

Is there a general way to pass the arguments in the timeout instead of specifying a,b,c ? I dont want the code to break if I chnage the arguments on the function definition.

Nir
  • 24,619
  • 25
  • 81
  • 117

2 Answers2

1

You can use arguments if you don't wanna specify a,b,c. and also use the wrapper function and call arguments inside that.

function wait_then_go  (a,b,c) {
    const arg = [...arguments];
    if (!go) window.setTimeout (() => wait_then_go (...args), 500);
}
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
0

Pass the function wait_then_go into setTimeout callback function, and pass ...arguments as params.

function wait_then_go  (a,b,c) {
  if (!go) window.setTimeout (function() {
    wait_then_go (...arguments)
  }, 500);
  // go here 
}
Radonirina Maminiaina
  • 6,958
  • 4
  • 33
  • 60
  • It looks like you have repeated a,b,c in the call so that does not answer my question. – Nir May 10 '19 at 11:41