You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaScript定时调用JSON API 如何对比前后数据判断金银价涨跌

金银价格涨跌走势判断实现方案

核心实现逻辑

  • 在全局作用域定义变量存储上一次接口返回的各类价格数据,作为对比基准
  • 每次接口请求成功后,先判断是否为首次拉取数据:首次拉取仅存储基准数据,不做涨跌判断
  • 非首次拉取时,将当前最新价格与上一次存储的基准数据逐一做差值对比,即可判断各品类价格走势
  • 走势判断完成后,将最新价格更新为基准数据,供下一次拉取对比使用

完整可运行代码

// 全局变量:存储上一次请求的金银价格数据,作为对比基准
let lastPriceData = null;

function rates_update(){
    $.ajax({
        url: 'https://data-asg.goldprice.org/dbXRates/USD,AED',
        cache: false,
        type: "GET",
        dataType: "json",
        success: function (data) {
            // 提取当前最新价格
            const currentPrices = {
                goldAedAsk: data['items'][0]['xauPrice'],
                goldUsdAsk: data['items'][1]['xauPrice'],
                silverUsdAsk: data['items'][1]['xagPrice'],
                silverAedAsk: data['items'][0]['xagPrice'],
                goldAed: data['items'][0]['xauPrice'],
                goldUsd: data['items'][1]['xauPrice'],
                silverUsd: data['items'][1]['xagPrice'],
                silverAed: data['items'][0]['xagPrice']
            }

            // 首次请求仅存储基准数据,无需判断涨跌
            if (!lastPriceData) {
                lastPriceData = currentPrices;
                return;
            }

            // 涨跌判断逻辑示例,可根据需求修改输出/交互方式
            // 黄金USD走势判断
            if (currentPrices.goldUsd > lastPriceData.goldUsd) {
                console.log('黄金(USD)价格上涨');
            } else if (currentPrices.goldUsd < lastPriceData.goldUsd) {
                console.log('黄金(USD)价格下跌');
            } else {
                console.log('黄金(USD)价格无变动');
            }

            // 黄金AED走势判断
            if (currentPrices.goldAed > lastPriceData.goldAed) {
                console.log('黄金(AED)价格上涨');
            } else if (currentPrices.goldAed < lastPriceData.goldAed) {
                console.log('黄金(AED)价格下跌');
            } else {
                console.log('黄金(AED)价格无变动');
            }

            // 白银USD走势判断
            if (currentPrices.silverUsd > lastPriceData.silverUsd) {
                console.log('白银(USD)价格上涨');
            } else if (currentPrices.silverUsd < lastPriceData.silverUsd) {
                console.log('白银(USD)价格下跌');
            } else {
                console.log('白银(USD)价格无变动');
            }

            // 白银AED走势判断
            if (currentPrices.silverAed > lastPriceData.silverAed) {
                console.log('白银(AED)价格上涨');
            } else if (currentPrices.silverAed < lastPriceData.silverAed) {
                console.log('白银(AED)价格下跌');
            } else {
                console.log('白银(AED)价格无变动');
            }

            // 更新基准数据,供下次对比使用
            lastPriceData = currentPrices;
        }
    });
}

// 定时拉取接口,当前间隔为1秒,可根据实际需求调整时间
setInterval(function(){ rates_update(); }, 1000);

注意事项

  • 当前1秒的请求频率较高,若不需要极致的实时性,可适当调大间隔时间,降低接口请求压力
  • 涨跌判断后的处理逻辑可根据业务需求修改,比如替换为页面提示、数值颜色变化等交互效果

内容的提问来源于stack exchange,提问作者Jawad Ajaz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 22:39:03