jquery/javascript - 从函数外部访问变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3546459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery/javascript - accessing variables from outside a function
提问by Hyman
I'm trying to use a variable value outside of the function it was defined in. Thought I. just needed to declare the variable outside the function but that doesn't cut it. Gotta be an easy one for those who know?
Fiddle Here
我试图在定义它的函数之外使用一个变量值。我想我只需要在函数之外声明变量,但这并不能削减它。对那些知道的人来说是一个容易的吗?
在这里小提琴
jQuery(document).ready(function() {
var readOut;
var readOut2;
$(document).mousemove(function(e) {
readOut1 = e.pageX;
readOut2 = e.pageY;
$('#var1').html(readOut1);
});
$('#var2').html(readOut2);
})?
Thanks to all, especially Andy E with the explaination and solution.
感谢所有人,尤其是 Andy E 的解释和解决方案。
采纳答案by Andy E
You're assigning to the variables via a callback function which is registered to an event handler. This means that when this runs:
您通过注册到事件处理程序的回调函数分配给变量。这意味着当它运行时:
$('#var2').html(readOut2);
readOut2has a value of undefinedbecause it hasn't been set by the mousemovehandler function yet. That handler won't fire until the current queued code stops executing and the user moves their mouse. You could define a function in the same scope as the variables, and call that function from the mousemovehandler.
readOut2有一个undefined值,因为它尚未被mousemove处理函数设置。在当前排队的代码停止执行并且用户移动鼠标之前,该处理程序不会触发。您可以在与变量相同的范围内定义一个函数,并从mousemove处理程序调用该函数。
Re: your comments on another answer, you could use a timer:
回复:您对另一个答案的评论,您可以使用计时器:
jQuery(document).ready(function() {
var readOut1;
var readOut2;
$(document).mousemove(function(e) {
readOut1 = e.pageX;
readOut2 = e.pageY;
$('#var1').html(readOut1);
});
window.setInterval(function () {
$('#var2').html(readOut2);
}, 300);
})?;
回答by Otar
I guess you want to track cursor coordinates, check out the updated source code:
我猜您想跟踪光标坐标,请查看更新的源代码:
jQuery(document).ready(function() {
var readOut1;
var readOut2;
$(document).mousemove(function(e) {
readOut1 = e.pageX;
readOut2 = e.pageY;
$('#var1').html(readOut1);
$('#var2').html(readOut2);
});
})?
回答by Sam
Seems like a timing problem.
好像是时间问题。
This line
这条线
$('#var2').html(readOut2);
is gonna get called at document.ready, while the mousemove event hasn't been called yet, so readOut2 will not have a value yet.
将在 document.ready 中被调用,而 mousemove 事件还没有被调用,所以 readOut2 还没有值。
回答by adamse
but want to use the value outside the on mousemove function
但想使用 on mousemove 函数之外的值
As the variables readOut1and readOut2might not be set before the mousemove event handler is run you will have to call any code that will use these variables from the mousemove handler.
由于在运行 mousemove 事件处理程序之前可能不会设置变量readOut1和readOut2,因此您必须从 mousemove 处理程序调用将使用这些变量的任何代码。
Example:
例子:
$(document).mousemove(function(e) {
readOut1 = e.pageX;
readOut2 = e.pageY;
doStuffWithReadOuts(/* possibly passing readouts as arguments instead... */);
});
function doStuffWithReadOuts() {
$('#var1').html(readOut1);
$('#var2').html(readOut2);
}

