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

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

问题描述

我要查询从脚本一个值,该值作为变量进行进一步的功能。这code返回零尽管值被设定。

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函数,但withous成功。

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.

如何设置全局变量

编辑:

我现在试图用延迟对象/承诺。我仍然有我需要从AJAX调用的价值之前,我初始化我的柜台的问题。我不能创建在的计数器对象完成部分,因为我不能再更改计数器(仍然没有全局变量)的值。目前,我有这样的:

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

如果我是你,我会去了解一下库code和整顿奇怪的动画零开始值。而据我所知,你不怕这样做。

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天全站免登陆