-3

I am new to Javascirpt. What exactly should I add to the code below so thatthis.callHandler('ok'); will only run after 10 seconds

Window_NameInput.prototype.processHandling = function() {
    if (this.isOpen() && this.active) {
        this.callHandler('ok');
    }
};

Any help is appreciated Thanks in advance

Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
Tosps
  • 73
  • 7
  • 1
    Possible duplicate of [How to set time delay in javascript](https://stackoverflow.com/questions/17883692/how-to-set-time-delay-in-javascript) and [Execute script after specific delay using JavaScript](https://stackoverflow.com/questions/24849) – adiga Mar 09 '19 at 06:18

1 Answers1

1

Wrap this.callHandler('ok') with a function and pass it to setTimeout(). Pass 10000 as second parameter because it takes time in milliseconds.

Window_NameInput.prototype.processHandling = function() {
    if (this.isOpen() && this.active) {
        setTimeout(() => this.callHandler('ok'),10000); 
    }
};
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
  • what my code will do is close a window inside a game. but after using your code, it close the whole game, instead of a window inside a game. Any ideas ? – Tosps Mar 09 '19 at 06:16
  • @Tosps Can't say anything without seeing your code. But my code only delay `this.callHandler('ok')` for 10 seconds. There might be some problem in `this.callHandler` – Maheer Ali Mar 09 '19 at 06:18
  • I think it's might be because I didn't clear timeout. how do you clear timeout for this? Thanks – Tosps Mar 11 '19 at 00:18
  • @Tosps You don't need to `clearTimeout` it will only occur once. but if you want to stop it before even happening once you can use `clearTimeout()`. I have updated the answer – Maheer Ali Mar 11 '19 at 00:22
  • Maybe because if (this.isOpen() && this.active) , it is always checking if the window is open , so when timeout it repeats itself and keeps closing too many times? – Tosps Mar 11 '19 at 00:29
  • I have a hard time figuring out the reason, if I don't set timeout , this.callHandler('ok'); runs once, if I set timeout it repeats – Tosps Mar 11 '19 at 00:34
  • if I put setTimeout(() => alert("alert") ,10000); it keeps alerting repeatedly – Tosps Mar 11 '19 at 00:47
  • @Tosps Then this mean you are calling this function repeatdly – Maheer Ali Mar 11 '19 at 00:47
  • I see what's happening. In my original code this.callHandler('ok') was supposed to repeat, but before it can run the second time, it already disabled the" if window is open" condition. So it won't run anymore. How come setTimeout(() => this.callHandler('ok'),10000); will not disable the " if window is open" condition the first time? insteading it just keeps going. – Tosps Mar 11 '19 at 01:13