One day, I encountered a simple if(confirm())
condition like this:
if(confirm('Some question')) {
agreed_function();
} else {
cancel_function();
}
function agreed_function() {
console.log('OK');
}
function cancel_function() {
console.log('Cancel');
}
Suddenly, I realized that I needed to call the cancel_function()
not just when the "Cancel" button was clicked, but also if the "OK" button wasn't clicked within 3 seconds. It seemed like a challenge, so I started tinkering with my code.
Despite my efforts, the interval part of the code didn't work as intended. Here's what I had:
var interval = setInterval(function() {
cancel_function();
}, 3000);
if(confirm('Some question')) {
clearInterval(interval);
agreed_function();
} else {
clearInterval(interval); // prevent calling cancel_function() every 3 seconds
cancel_function();
}
function agreed_function() {
console.log('OK');
}
function cancel_function() {
console.log('Cancel');
}
I found myself stuck, wondering how to overcome this obstacle. Any ideas on how to resolve this dilemma?