使用jquery比较先前的数据和当前数据值

时间:2017-02-08 03:39:18

标签: javascript jquery

我在下面有这行代码。

var previousGoldValue;
var currentGoldValue;
var previousSilverValue;
var currentSilverValue;

function doRefresh() {
  var goldprice = $('[data-metal="gold"]').html();
  previousGoldValue = localStorage.getItem("previousGoldValue");
  currentGoldValue = goldprice;
  var silverprice = $('[data-metal="silver"]').html();
  previousSilverValue = localStorage.getItem("previousSilverValue");
  currentSilverValue = silverprice;
  if (currentGoldValue < previousGoldValue) {

    $('.price-status.gold').removeClass("status-up").addClass("status-down");
    $('.goldfeed-widget .gold-status .status').removeClass("status-up").addClass("status-down");
  } else {
    $('.price-status.gold').removeClass("status-down").addClass("status-up");
    $('.goldfeed-widget .gold-status .status').removeClass("status-down").addClass("status-up");
  }

  if (currentSilverValue < previousSilverValue) {
    $('.price-status.silver').removeClass("status-down").addClass("status-up");
    $('.goldfeed-widget .silver-status .status').removeClass("status-down").addClass("status-up");
  } else {
    $('.price-status.silver').removeClass("status-up").addClass("status-down");
    $('.goldfeed-widget .silver-status .status').removeClass("status-up").addClass("status-down");
  }
  $('.price-status.gold').html(goldprice + "/oz");
  $('.price-status.silver').html(silverprice + "/oz");
  localStorage.setItem("previousGoldValue", currentGoldValue);
  localStorage.setItem("previousSilverValue", currentSilverValue);
  console.log("previous: " + previousGoldValue);
  console.log("current: " + currentGoldValue);
}

setInterval(doRefresh,1000);

我需要将以前的数据与当前数据进行比较,如果它大于或小于以前的数据。但我得到的是当前和以前数据的相同数据

previous: 1,616.75
current: 1,616.75

0 个答案:

没有答案