【问题标题】:Getting a value from json to insert in another function js从json获取值以插入另一个函数js
【发布时间】:2014-01-13 21:36:12
【问题描述】:

我想将一个值从一个函数传递给一个“值:”来制作动画

传到这里具体说 "Value: 12" 替换 json 结果的值

var updateJackpots = function()
{
    $.support.cors = true;
    $.ajax({
        url: 'https://www.ganegrande.do/ajax/getjackpots',
        cache: false,
        crossDomain: true,
        dataType: 'json',
        success: function(data) {

            var total = 0; $('#total').remove();

            //update the jackpots
            $(data).each(function(i, el)
            {   
                total += getMoneyValue(el.formatted_value);
            });

            //update the total
            $('<div id="total"/>').html(''+commaFormatted(parseFloat(total).toFixed(2))).appendTo('body');


            //update jackpots again after 30 seconds
            setTimeout(updateJackpots, 864000000);
        },
        error: function(i,a,e)
        {
            alert(i + a + e);
        }
    });
}; updateJackpots();
...

function loadCounter(){
    customCounter = new Counter("customCounter", {
        ...,
        value : 12 <!-- HERE SHOULD GO THE JSON RESULT "2544423.11" AUTOMATICALLY -->
    });

    customTimerId = window.setInterval(function(){
        ...
    }, 2050); 
}

loadCounter();

});

完整代码如下:

http://jsfiddle.net/JoelGarcia/jL3fn/14/

【问题讨论】:

  • 你可以只给我们看重要的代码,这样你只会迷惑想要帮助你的人
  • 好吧,对不起!!!我太累了兄弟。我会记账谢谢
  • 小心:10000000000000000000000000000000000000000
  • 看看 jQuery 的 $.ajax() api.jquery.com/jquery.ajax。您可以使用它从网络服务器获取 JSON(或任何其他数据)。请注意,它可能会被您的浏览器阻止到其他服务器以防止 XSS 攻击。
  • 这些数字只是为了测试“Igor Benikov”

标签: javascript jquery json css animation


【解决方案1】:

最后,我认为这就是你想要的。我添加了一个counterCreated 变量,所以当ajax 调用返回时,如果没有计数器,它会创建一个,否则它会更新当前的计数器值。见http://jsfiddle.net/jL3fn/21/

或者,您可以创建值为 0 的计数器,然后在 ajax 调用返回时更新它。见http://jsfiddle.net/jL3fn/22/

【讨论】:

  • 不,我想为 json 结果“2544424.09”设置动画,但要设置动画,我需要获取值并将其放入值中:12
  • 看看代码,我想我已经完成了你的要求。
  • 抱歉 Fred 还没有,在哪里说“latestUpdate”我想看看 json 结果的数量,这一切都是可能的?
  • 当你再次点击 updateJackPots 计时器时,唯一的问题是它实际上只会在前一个计时器下面创建一个新计时器
  • 特别感谢FRED。最后!!我得到了我的结果,你太棒了。现在正在按我的意愿运行
【解决方案2】:

不完全确定这里的最终目标是什么,但我认为您想加载累积奖金总数,然后开始按某个数字计数,不知道除此之外还有什么。

我向updateJackPots 方法添加了一个non async 调用,以便等到值被填充然后创建计数器。

jsFiddle

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多