I've got the following code:
我有以下代码:
$("#avvia_cronometro").click(function() { var tempo = setInterval(function() { cronometro(); $("#tempo_cronometro").html((h)+":"+(min)+":"+(sec)); }, 1000);});$("#stop_cronometro").click(function() { clearInterval(tempo);});function cronometro() { if (sec == 59) { min +=1; if (min == 59) { h+=1; min=0; } sec=0; } sec+=1;}
When I click on #stop_cronometro
it doesn't work and it says:
当我点击#stop_cronometro时,它不起作用,它说:
Uncaught ReferenceError: tempo is not defined
How can I fix?
我该怎么办?
If I click on #avvia_cronometro
it starts with the time so it's work.
如果我点击#avvia_cronometro它会从时间开始,所以它的工作。
3 个解决方案
#1
1
Because there is no variable tempo
that exist in global scope (or any scope that the stop click handler can reach).
因为全局范围(或停止点击处理程序可以到达的任何范围)中不存在变量速度。
When you declare a variable with var
inside a function that variable gets deleted when the function returns:
在函数内部声明变量时,变量在函数返回时被删除:
function foo () { var bar = 1;}foo();console.log(bar); // uncaught reference error - "bar" doesn't exist
If you need a global variable, use it without the var
:
如果需要全局变量,请在不使用var的情况下使用它:
function foo () { bar = 1;}foo();console.log(bar); // prints 1
However, I would generally not recommend this since it looks like an error to future maintainers. Instead, declare the global variable explicitly in global scope to clearly show your intention:
但是,我一般不会推荐这个,因为它对未来的维护者来说似乎是一个错误。相反,在全局范围内明确声明全局变量以清楚地表明您的意图:
var bar = null;function foo() { bar = 1;}foo();console.log(bar); // prints 1
#2
1
Well, it happens because the variable scope inside separate functions, when a variable is declared inside a function, it will be only acessible to itself and the child functions.
好吧,它发生的原因是变量范围在单独的函数内部,当一个变量在函数内部声明时,它只能访问自身和子函数。
In you case I suggest you make "tempo" variable global:
在你的情况下,我建议你使“节奏”变量全局:
var tempo = null;$("#avvia_cronometro").click(function() { tempo = setInterval(function() { cronometro(); $("#tempo_cronometro").html((h)+":"+(min)+":"+(sec)); }, 1000);});$("#stop_cronometro").click(function() { clearInterval(tempo);});function cronometro() { if (sec == 59) { min +=1; if (min == 59) { h+=1; min=0; } sec=0; } sec+=1;}
#3
0
Because you are clearing interval before it starts executing. Handle tempo undefined in if. SetInterval is an asynchronus call.
因为您在开始执行之前清除间隔。在if中处理未定义的速度。 SetInterval是一个异步调用。
#1
1
Because there is no variable tempo
that exist in global scope (or any scope that the stop click handler can reach).
因为全局范围(或停止点击处理程序可以到达的任何范围)中不存在变量速度。
When you declare a variable with var
inside a function that variable gets deleted when the function returns:
在函数内部声明变量时,变量在函数返回时被删除:
function foo () { var bar = 1;}foo();console.log(bar); // uncaught reference error - "bar" doesn't exist
If you need a global variable, use it without the var
:
如果需要全局变量,请在不使用var的情况下使用它:
function foo () { bar = 1;}foo();console.log(bar); // prints 1
However, I would generally not recommend this since it looks like an error to future maintainers. Instead, declare the global variable explicitly in global scope to clearly show your intention:
但是,我一般不会推荐这个,因为它对未来的维护者来说似乎是一个错误。相反,在全局范围内明确声明全局变量以清楚地表明您的意图:
var bar = null;function foo() { bar = 1;}foo();console.log(bar); // prints 1
#2
1
Well, it happens because the variable scope inside separate functions, when a variable is declared inside a function, it will be only acessible to itself and the child functions.
好吧,它发生的原因是变量范围在单独的函数内部,当一个变量在函数内部声明时,它只能访问自身和子函数。
In you case I suggest you make "tempo" variable global:
在你的情况下,我建议你使“节奏”变量全局:
var tempo = null;$("#avvia_cronometro").click(function() { tempo = setInterval(function() { cronometro(); $("#tempo_cronometro").html((h)+":"+(min)+":"+(sec)); }, 1000);});$("#stop_cronometro").click(function() { clearInterval(tempo);});function cronometro() { if (sec == 59) { min +=1; if (min == 59) { h+=1; min=0; } sec=0; } sec+=1;}
#3
0
Because you are clearing interval before it starts executing. Handle tempo undefined in if. SetInterval is an asynchronus call.
因为您在开始执行之前清除间隔。在if中处理未定义的速度。 SetInterval是一个异步调用。