Auto click a button after 10 seconds

Dialog input plugin
https://forums.rpgmakerweb.com/index.php?threads/keyboard-input-dialog.66908/

I want
after open dialog input , auto click the Cancel button after 10 seconds.

I added this code

if(SceneManager._scene instanceof Scene_InputDialog) {
setTimeout(function () {document.getElementById("inputDialog-CancelBtn").click();}, 10000);
}

everything works fine but it will cause the error if I click Ok button during the 10 seconds.

Uncaught Type error: Cannot read propety 'click' of null

How should I edit the code I added so that during the 10 seconds, if the OK button is clicked, don't auto click Cancel button anymore

Any help is appreciated
You need to clear the timeout if the ok button is pressed:

https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/clearTimeout
I am actually new to javascript
this link below is the whole orginal plugin script without adding my code
https://raw.githubusercontent.com/biud436/MV/master/RS_InputDialog.js


so in my project I changed the code I added:

const myTimeout;

if(SceneManager._scene instanceof Scene_InputDialog) {
myTimeout = setTimeout(function () {
document.getElementById("inputDialog-CancelBtn").click();
}, 10000);
}


and changed the original script from:

okButton.addEventListener('click', function (e) { okFunc(); e.preventDefault(); }, false);


to:

okButton.addEventListener('click', function (e) { okFunc(); e.preventDefault(); clearTimeout(myTimeout)}, false);


now it immediately auto clicks the button instead of the auto click in 10 seconds

did I change the code right ?


I am actually new to javascript
this link below is the whole orginal plugin script without adding my code
https://raw.githubusercontent.com/biud436/MV/master/RS_InputDialog.js


so in my project I changed the code I added:

const myTimeout;

if(SceneManager._scene instanceof Scene_InputDialog) {
myTimeout = setTimeout(function () {
document.getElementById("inputDialog-CancelBtn").click();
}, 10000);
}


and changed the original script from:

okButton.addEventListener('click', function (e) { okFunc(); e.preventDefault(); }, false);


to:

okButton.addEventListener('click', function (e) { okFunc(); e.preventDefault(); clearTimeout(myTimeout)}, false);


now it immediately auto clicks the button instead of the auto click in 10 seconds

did I change the code right ?
If you can use the CODE tags, it'll help to read it easier 🙂

You probably have an error showing in the console (press F8 during playtest to open it). The reason is:
Code:
const myTimeout; // error: const must be initialized with a value
Variables declared with 'const' must be initialized with a value and cannot be reassigned. In this case, you need to reassign 'myTimeout' to a different value, so declare the variable with 'let' instead.

Some examples:
Code:
const myNum; // not allowed
const yourNum = 2; // OK
yourNum = 3; // not allowed, can't reassign
let hisNum; // OK
let herNum = 4; //OK
herNum = 'hello world'; // OK

Another solution could be to check whether the button exists or not before trying to click it:
Code:
if(SceneManager._scene instanceof Scene_InputDialog) {
   myTimeout = setTimeout(function () {
      const cancelBtn = document.getElementById("inputDialog-CancelBtn");
      if (cancelBtn) cancelBtn.click();
   }, 10000);
}
If you can use the CODE tags, it'll help to read it easier 🙂

You probably have an error showing in the console (press F8 during playtest to open it). The reason is:
Code:
const myTimeout; // error: const must be initialized with a value
Variables declared with 'const' must be initialized with a value and cannot be reassigned. In this case, you need to reassign 'myTimeout' to a different value, so declare the variable with 'let' instead.

Some examples:
Code:
const myNum; // not allowed
const yourNum = 2; // OK
yourNum = 3; // not allowed, can't reassign
let hisNum; // OK
let herNum = 4; //OK
herNum = 'hello world'; // OK

Another solution could be to check whether the button exists or not before trying to click it:
Code:
if(SceneManager._scene instanceof Scene_InputDialog) {
   myTimeout = setTimeout(function () {
      const cancelBtn = document.getElementById("inputDialog-CancelBtn");
      if (cancelBtn) cancelBtn.click();
   }, 10000);
}

Thank you! it works

Original thread: https://forums.rpgmakerweb.com/threads/auto-click-a-button-after-10-seconds.103803/