How to clearTimeout onclick?
I want to clear all setTimeouts in the loadStory function when the user clicks on a button with an a href link that brings them to the next webpage. However, the stopTimeout function doesn't get executed and the loadStory function continues loading onto the next page. How do I clear all setTimeouts to prevent it from continuing execution onto the next webpage? //In TypeScript var one, two, three; function loadStory(){ function first() { return new Promise(function(resolve, reject) { one = setTimeout((function() { $('#p1').append('The fox was riding a bicycle.'); resolve("Working"); }), 500); }); } function second() { return new Promise(function(resolve, reject) { two = setTimeout((function() { $('#p2').append('It saw a tiger.'); resolve("Working"); }), 1200); }); } function third() { return new Promise(function(resolve, reject) { three = setTimeout((function() { $('#p3').append('The fox ran away.'); resolve("Working"); }), 1200); }); } first().then(second).then(third); } function stopTimeout(){ clearTimeout(one);clearTimeout(two);clearTimeout(three); alert("timeout executed"); } <!--In HTML--> <button onclick="loadStory()">Next</button> <button><a href="page2.html" onclick="loadStory.stopTimeout()">Next</a></button>