在匿名函数中设置全局变量 [英] Set global variable within anonymous function

查看:34
本文介绍了在匿名函数中设置全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从脚本中查询一个值并将该值用作进一步函数的变量.尽管设置了值,但此代码返回零.

I want to query a value from a script and use the value as variable for further functions. This code returns zero despite a value is set.

var total = 0;
var jqxhr = $.getJSON("number.php?jsonp=?", function(data) {
    total = data[0]['Total']; // e.g 1234567
});
alert (total); // gives zero

我读到问题可能出在异步调用上,并且我的警报在匿名函数之前执行.我也尝试使用某种 setter 函数,但没有成功.

I read that the problem could be with the asynchronous call and that my alert is executed before the anonymous function. I also tried to use some kind of setter function but withous success.

如何设置全局变量total?

我现在尝试使用延迟对象/承诺.我仍然有一个问题,即在初始化计数器之前我需要来自 AJAX 调用的值.我无法在 done 部分创建计数器对象,因为我以后无法更改计数器的值(仍然没有全局变量).目前我有这个:

I now tried to use deferred objects / promises. I still have the problem that I need the value from the AJAX call before I initialise my counter. I cannot create the counter object in the done section because I cannot later change the value of the counter (still no global variable). Currently I have this:

var counter = $('.counter').jOdometer({
    counterStart: '0',
    numbersImage: '/img/jodometer-numbers-24pt.png', 
    widthNumber: 32,
    heightNumber: 54,
    spaceNumbers: 0, 
    offsetRight:-10,
    speed:10000,
    delayTime: 300,
    maxDigits: 10,
});

function update_odometer() { 
    var jqxhr = $.getJSON("/number.php?jsonp=?")
        .done(function(data){
            console.log(data);
            total = data['Total'];
            counter.goToNumber(total);
        })
        .fail(function(data){
            console.log(data);
        });
};

update_odometer();
setInterval(update_odometer, 60000);

如果我用零初始化计数器,那么会有一个奇怪的开始动画,它会上下跳跃.如果我能得到实数而不是零,一切都会好起来的.或者我应该完全切换到同步调用?我该怎么做?还是回调是更好的解决方案?

If I initialize the counter with zero then there is a strange start animations which jumps up and down. If I could get the real number instead of zero everything would work fine. Or should I completely switch to a synchronous call? How would I do that? Or are callbacks the better solution?

推荐答案

我花了一些时间才明白您的问题.我仍然不确定这是否会有所帮助.

Took me a while to understand your problem. Still I'm not sure whether this would help or not.

var counter = null;
function update_odometer() {
    var xhr = $.getJSON("/number.php?jsonp=?").done(function(data) {
        if (counter === null) {
            var counter = $('.counter').jOdometer({
            counterStart: data['Total'],
            numbersImage: '/img/jodometer-numbers-24pt.png', 
            widthNumber: 32,
            heightNumber: 54,
            spaceNumbers: 0, 
            offsetRight:-10,
            speed:10000,
            delayTime: 300,
            maxDigits: 10,
            });
        } else {
            counter.goToNumber(data['Total']);
        }
    });
}

如果我是你,我会查看库代码并纠正零起始值的奇怪动画.我知道你并不害怕这样做.

If I were you I would look into library code and rectify the strange animation for zero start value. And I understand that you are not afraid to do that.

这篇关于在匿名函数中设置全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆