I need someone to tell me how to cancel or clear the timer on this function.
//Html
<a id="button" data-url="http://url.com">Click me!</a>
Redirecting in <span id="timer"></span> seconds...
<a href="javascript:void(0)" class="cancel">Cancel</a>
// jS
$('a#button').click(function() {
var url = $(this).attr("data-url");
if( url.indexOf("http://")!==0 ){
url = "http://"+url;
}
var seconds = 5,
el = $('#timer')
el.text(seconds)
setTimeout(function countdown() {
seconds--
el.text(seconds)
if (seconds > 0) {
setTimeout(countdown, 1000)
}
else {
window.open( url , "_self" )
}
}, 1000)
})
$('a.cancel').click(function(){
clearTimeout(countdown);
});
Also tell me what I"m doing wrong and why this isn't working.