简体   繁体   English

jquery / javascript - 从函数外部访问变量

[英]jquery/javascript - accessing variables from outside a function

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. 我试图在它定义的函数之外使用一个变量值。思想I.只需要在函数外部声明变量但不会削减它。 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的解释和解决方案

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);

readOut2 has a value of undefined because it hasn't been set by the mousemove handler function yet. readOut2的值为undefined,因为它还没有被mousemove处理函数设置。 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 mousemove handler. 您可以在与变量相同的范围内定义函数,并从mousemove处理程序中调用该函数。

Re: your comments on another answer, you could use a timer: Re:你对另一个答案的评论,你可以使用一个计时器:

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);
})​;

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);
    });

})​

http://jsfiddle.net/xSa2T/2/ http://jsfiddle.net/xSa2T/2/

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还没有值。

but want to use the value outside the on mousemove function 但是想要使用mousemove函数之外的值

As the variables readOut1 and readOut2 might 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. 由于变量readOut1readOut2可能在运行mousemove事件处理程序之前未设置,因此您必须调用将使用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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM