Javascript Cancel SetTimeout after button is clicked












0















There is an ok button and a cancel button



The code below will do this:



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



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


If I click the ok button during those 10 seoconds , I will get this error below .( because after 10 seconds, it will try to auto click the cancel button which doesn't exist anymore; If Ok button is clicked, the cancel button will disappear)



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



How should I clear the SetTimeout if the Ok button is clicked ?
Thanks in advance for any help



If I need to add reference to the OK button handler, how should I add it.
below is my other codes.




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



this._okFunc = okFunc;
this._cancelFunc = cancelFunc;
};











share|improve this question




















  • 2





    w3schools.com/jsref/met_win_cleartimeout.asp

    – Wiktor Zychla
    Dec 29 '18 at 13:28











  • clearTimeout

    – Prashant Pimpale
    Dec 29 '18 at 13:28
















0















There is an ok button and a cancel button



The code below will do this:



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



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


If I click the ok button during those 10 seoconds , I will get this error below .( because after 10 seconds, it will try to auto click the cancel button which doesn't exist anymore; If Ok button is clicked, the cancel button will disappear)



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



How should I clear the SetTimeout if the Ok button is clicked ?
Thanks in advance for any help



If I need to add reference to the OK button handler, how should I add it.
below is my other codes.




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



this._okFunc = okFunc;
this._cancelFunc = cancelFunc;
};











share|improve this question




















  • 2





    w3schools.com/jsref/met_win_cleartimeout.asp

    – Wiktor Zychla
    Dec 29 '18 at 13:28











  • clearTimeout

    – Prashant Pimpale
    Dec 29 '18 at 13:28














0












0








0








There is an ok button and a cancel button



The code below will do this:



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



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


If I click the ok button during those 10 seoconds , I will get this error below .( because after 10 seconds, it will try to auto click the cancel button which doesn't exist anymore; If Ok button is clicked, the cancel button will disappear)



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



How should I clear the SetTimeout if the Ok button is clicked ?
Thanks in advance for any help



If I need to add reference to the OK button handler, how should I add it.
below is my other codes.




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



this._okFunc = okFunc;
this._cancelFunc = cancelFunc;
};











share|improve this question
















There is an ok button and a cancel button



The code below will do this:



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



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


If I click the ok button during those 10 seoconds , I will get this error below .( because after 10 seconds, it will try to auto click the cancel button which doesn't exist anymore; If Ok button is clicked, the cancel button will disappear)



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



How should I clear the SetTimeout if the Ok button is clicked ?
Thanks in advance for any help



If I need to add reference to the OK button handler, how should I add it.
below is my other codes.




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



this._okFunc = okFunc;
this._cancelFunc = cancelFunc;
};








javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 29 '18 at 13:38







Mary Clair

















asked Dec 29 '18 at 13:24









Mary ClairMary Clair

95




95








  • 2





    w3schools.com/jsref/met_win_cleartimeout.asp

    – Wiktor Zychla
    Dec 29 '18 at 13:28











  • clearTimeout

    – Prashant Pimpale
    Dec 29 '18 at 13:28














  • 2





    w3schools.com/jsref/met_win_cleartimeout.asp

    – Wiktor Zychla
    Dec 29 '18 at 13:28











  • clearTimeout

    – Prashant Pimpale
    Dec 29 '18 at 13:28








2




2





w3schools.com/jsref/met_win_cleartimeout.asp

– Wiktor Zychla
Dec 29 '18 at 13:28





w3schools.com/jsref/met_win_cleartimeout.asp

– Wiktor Zychla
Dec 29 '18 at 13:28













clearTimeout

– Prashant Pimpale
Dec 29 '18 at 13:28





clearTimeout

– Prashant Pimpale
Dec 29 '18 at 13:28












2 Answers
2






active

oldest

votes


















1














You can just check if document.getElementById("inputDialog-CancelBtn") is not null before you .click()



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


To clear the timeout the Ok button click handler needs to have a reference to the timeout, it's easier to just have that null check






share|improve this answer


























  • Thanks I am new to Javascript.

    – Mary Clair
    Dec 29 '18 at 13:35











  • I have updated the code, how do I add reference to the timeout ?

    – Mary Clair
    Dec 29 '18 at 13:39











  • If you are doing the null check, you don't need to cancel the timeout.

    – Ionut Achim
    Dec 29 '18 at 13:44











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

    – Mary Clair
    Dec 29 '18 at 14:07











  • is that how I should edit ?

    – Mary Clair
    Dec 29 '18 at 14:08



















1














const myTimeout;

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


then you can cancel it with:



clearTimeout(myTimeout)





share|improve this answer
























  • Thanks, I am new to Javascript. I have updated the code, where should I add the clearTimeout(myTimeout)

    – Mary Clair
    Dec 29 '18 at 13:42











  • in the OK button handler function

    – distante
    Dec 29 '18 at 13:44











  • This is the link to the original whole script without adding my code, raw.githubusercontent.com/biud436/MV/master/RS_InputDialog.js I am not sure which one is the handler function, or where exactly should I put the clearTimeout(myTimeout)

    – Mary Clair
    Dec 29 '18 at 14:41











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53969965%2fjavascript-cancel-settimeout-after-button-is-clicked%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You can just check if document.getElementById("inputDialog-CancelBtn") is not null before you .click()



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


To clear the timeout the Ok button click handler needs to have a reference to the timeout, it's easier to just have that null check






share|improve this answer


























  • Thanks I am new to Javascript.

    – Mary Clair
    Dec 29 '18 at 13:35











  • I have updated the code, how do I add reference to the timeout ?

    – Mary Clair
    Dec 29 '18 at 13:39











  • If you are doing the null check, you don't need to cancel the timeout.

    – Ionut Achim
    Dec 29 '18 at 13:44











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

    – Mary Clair
    Dec 29 '18 at 14:07











  • is that how I should edit ?

    – Mary Clair
    Dec 29 '18 at 14:08
















1














You can just check if document.getElementById("inputDialog-CancelBtn") is not null before you .click()



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


To clear the timeout the Ok button click handler needs to have a reference to the timeout, it's easier to just have that null check






share|improve this answer


























  • Thanks I am new to Javascript.

    – Mary Clair
    Dec 29 '18 at 13:35











  • I have updated the code, how do I add reference to the timeout ?

    – Mary Clair
    Dec 29 '18 at 13:39











  • If you are doing the null check, you don't need to cancel the timeout.

    – Ionut Achim
    Dec 29 '18 at 13:44











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

    – Mary Clair
    Dec 29 '18 at 14:07











  • is that how I should edit ?

    – Mary Clair
    Dec 29 '18 at 14:08














1












1








1







You can just check if document.getElementById("inputDialog-CancelBtn") is not null before you .click()



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


To clear the timeout the Ok button click handler needs to have a reference to the timeout, it's easier to just have that null check






share|improve this answer















You can just check if document.getElementById("inputDialog-CancelBtn") is not null before you .click()



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


To clear the timeout the Ok button click handler needs to have a reference to the timeout, it's easier to just have that null check







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 29 '18 at 14:16

























answered Dec 29 '18 at 13:28









Ionut AchimIonut Achim

66436




66436













  • Thanks I am new to Javascript.

    – Mary Clair
    Dec 29 '18 at 13:35











  • I have updated the code, how do I add reference to the timeout ?

    – Mary Clair
    Dec 29 '18 at 13:39











  • If you are doing the null check, you don't need to cancel the timeout.

    – Ionut Achim
    Dec 29 '18 at 13:44











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

    – Mary Clair
    Dec 29 '18 at 14:07











  • is that how I should edit ?

    – Mary Clair
    Dec 29 '18 at 14:08



















  • Thanks I am new to Javascript.

    – Mary Clair
    Dec 29 '18 at 13:35











  • I have updated the code, how do I add reference to the timeout ?

    – Mary Clair
    Dec 29 '18 at 13:39











  • If you are doing the null check, you don't need to cancel the timeout.

    – Ionut Achim
    Dec 29 '18 at 13:44











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

    – Mary Clair
    Dec 29 '18 at 14:07











  • is that how I should edit ?

    – Mary Clair
    Dec 29 '18 at 14:08

















Thanks I am new to Javascript.

– Mary Clair
Dec 29 '18 at 13:35





Thanks I am new to Javascript.

– Mary Clair
Dec 29 '18 at 13:35













I have updated the code, how do I add reference to the timeout ?

– Mary Clair
Dec 29 '18 at 13:39





I have updated the code, how do I add reference to the timeout ?

– Mary Clair
Dec 29 '18 at 13:39













If you are doing the null check, you don't need to cancel the timeout.

– Ionut Achim
Dec 29 '18 at 13:44





If you are doing the null check, you don't need to cancel the timeout.

– Ionut Achim
Dec 29 '18 at 13:44













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

– Mary Clair
Dec 29 '18 at 14:07





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

– Mary Clair
Dec 29 '18 at 14:07













is that how I should edit ?

– Mary Clair
Dec 29 '18 at 14:08





is that how I should edit ?

– Mary Clair
Dec 29 '18 at 14:08













1














const myTimeout;

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


then you can cancel it with:



clearTimeout(myTimeout)





share|improve this answer
























  • Thanks, I am new to Javascript. I have updated the code, where should I add the clearTimeout(myTimeout)

    – Mary Clair
    Dec 29 '18 at 13:42











  • in the OK button handler function

    – distante
    Dec 29 '18 at 13:44











  • This is the link to the original whole script without adding my code, raw.githubusercontent.com/biud436/MV/master/RS_InputDialog.js I am not sure which one is the handler function, or where exactly should I put the clearTimeout(myTimeout)

    – Mary Clair
    Dec 29 '18 at 14:41
















1














const myTimeout;

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


then you can cancel it with:



clearTimeout(myTimeout)





share|improve this answer
























  • Thanks, I am new to Javascript. I have updated the code, where should I add the clearTimeout(myTimeout)

    – Mary Clair
    Dec 29 '18 at 13:42











  • in the OK button handler function

    – distante
    Dec 29 '18 at 13:44











  • This is the link to the original whole script without adding my code, raw.githubusercontent.com/biud436/MV/master/RS_InputDialog.js I am not sure which one is the handler function, or where exactly should I put the clearTimeout(myTimeout)

    – Mary Clair
    Dec 29 '18 at 14:41














1












1








1







const myTimeout;

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


then you can cancel it with:



clearTimeout(myTimeout)





share|improve this answer













const myTimeout;

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


then you can cancel it with:



clearTimeout(myTimeout)






share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 29 '18 at 13:28









distantedistante

1,0001334




1,0001334













  • Thanks, I am new to Javascript. I have updated the code, where should I add the clearTimeout(myTimeout)

    – Mary Clair
    Dec 29 '18 at 13:42











  • in the OK button handler function

    – distante
    Dec 29 '18 at 13:44











  • This is the link to the original whole script without adding my code, raw.githubusercontent.com/biud436/MV/master/RS_InputDialog.js I am not sure which one is the handler function, or where exactly should I put the clearTimeout(myTimeout)

    – Mary Clair
    Dec 29 '18 at 14:41



















  • Thanks, I am new to Javascript. I have updated the code, where should I add the clearTimeout(myTimeout)

    – Mary Clair
    Dec 29 '18 at 13:42











  • in the OK button handler function

    – distante
    Dec 29 '18 at 13:44











  • This is the link to the original whole script without adding my code, raw.githubusercontent.com/biud436/MV/master/RS_InputDialog.js I am not sure which one is the handler function, or where exactly should I put the clearTimeout(myTimeout)

    – Mary Clair
    Dec 29 '18 at 14:41

















Thanks, I am new to Javascript. I have updated the code, where should I add the clearTimeout(myTimeout)

– Mary Clair
Dec 29 '18 at 13:42





Thanks, I am new to Javascript. I have updated the code, where should I add the clearTimeout(myTimeout)

– Mary Clair
Dec 29 '18 at 13:42













in the OK button handler function

– distante
Dec 29 '18 at 13:44





in the OK button handler function

– distante
Dec 29 '18 at 13:44













This is the link to the original whole script without adding my code, raw.githubusercontent.com/biud436/MV/master/RS_InputDialog.js I am not sure which one is the handler function, or where exactly should I put the clearTimeout(myTimeout)

– Mary Clair
Dec 29 '18 at 14:41





This is the link to the original whole script without adding my code, raw.githubusercontent.com/biud436/MV/master/RS_InputDialog.js I am not sure which one is the handler function, or where exactly should I put the clearTimeout(myTimeout)

– Mary Clair
Dec 29 '18 at 14:41


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53969965%2fjavascript-cancel-settimeout-after-button-is-clicked%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

compose and upload a new article using a custom form

How to correct the classpath of spring boot application so that it contains a single, compatible version of...