Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
循环后的Javascript未定义变量_Javascript_Html_Variables_Loops - Fatal编程技术网

循环后的Javascript未定义变量

循环后的Javascript未定义变量,javascript,html,variables,loops,Javascript,Html,Variables,Loops,我有一个倒计时脚本,可以重定向到一个文件。它有一个循环,当变量运行一次时,它就没有被定义 如何保持url变量保持其值 <a id="" onClick="doTimer('http://www.domain.com/downloadfile.php?photo=foo.jpg')" href="#"><button id="download">Download this photo</button></a> var

我有一个倒计时脚本,可以重定向到一个文件。它有一个循环,当变量运行一次时,它就没有被定义

如何保持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) 
            }
        }

var定时器_为_on=0;
var countdownfrom=5
var currentsecond=document.getElementById('countdown')。innerHTML=countdownfrom+1
函数countredirect(url)
{ 
如果(当前秒!=1)
{
当前秒-=1
document.getElementById('countdown')。innerHTML=currentsecond;
} 
其他的
{ 
window.location=url
返回
} 
setTimeout(“countredirect()”,1000)
}
函数doTimer(url)
{
如果(!计时器打开)
{
document.getElementById('download').innerHTML=“您的下载在几秒钟内开始”;
定时器_为_开=1;
countredirect(url)
}
}
您没有向
countredirect
函数传递任何参数

将字符串传递给
setTimeout
setInterval
通常是个坏主意(会给您带来各种范围问题)。改为传递函数:

setTimeout(function() {
    countredirect(url);
}, 1000);
在较新的浏览器(或使用shim)中,您还可以使用(
bind
返回一个新函数):

您没有向
countredirect
函数传递任何参数

将字符串传递给
setTimeout
setInterval
通常是个坏主意(会给您带来各种范围问题)。改为传递函数:

setTimeout(function() {
    countredirect(url);
}, 1000);
在较新的浏览器(或使用shim)中,您还可以使用(
bind
返回一个新函数):


重新安排功能的另一种方法:

setTimeout(countredirect.bind(null, url), 1000);

重新安排功能的另一种方法:

setTimeout(countredirect.bind(null, url), 1000);

哪个变量未定义?切勿将字符串传递给
setInterval()
setTimeout()
。这样做与使用
eval()
一样糟糕,而且在使用变量时会导致代码无法读取,甚至可能不安全,因为您需要将变量插入字符串中,而不是传递实际的变量。正确的解决方案是
setInterval(function(){/*您的代码*)},毫秒)。这同样适用于
setTimeout()
。如果您只想在没有任何参数的情况下调用单个函数,还可以直接传递函数名:
setInterval(someFunction,毫秒)
(请注意,函数名后面没有
()
)我首先在dotimer函数中使用的url变量。哪个变量未定义?切勿将字符串传递给
setInterval()
setTimeout()
。这样做与使用
eval()
一样糟糕,而且在使用变量时会导致代码无法读取,甚至可能不安全,因为您需要将变量插入字符串中,而不是传递实际的变量。正确的解决方案是
setInterval(function(){/*您的代码*)},毫秒)。这同样适用于
setTimeout()
。如果您只想在没有任何参数的情况下调用单个函数,还可以直接传递函数名:
setInterval(someFunction,毫秒)
(注意函数名后面没有
()
)我首先在dotimer函数中使用的url变量。