我有一个重定向到文件的倒计时脚本。它有一个循环,并且当它运行一次时变量会变得未定义。
我怎样才能让 url 变量保持它的值?
<a id="" onClick="doTimer('http://www.domain.com/downloadfile.php?photo=foo.jpg')" href="#"><button id="download">Download this photo</button></a>
var timer_is_on=0;
var countdownfrom=5
var currentsecond=document.getElementById('countdown').innerHTML=countdownfrom+1
function countredirect(url)
{
if (currentsecond!=1)
{
currentsecond-=1
document.getElementById('countdown').innerHTML = currentsecond;
}
else
{
window.location=url
return
}
setTimeout("countredirect()",1000)
}
function doTimer(url)
{
if(!timer_is_on)
{
document.getElementById('download').innerHTML="Your download starts in <span id=\"countdown\"></span>seconds";
timer_is_on=1;
countredirect(url)
}
}