为什么不更新此代码块中的字符串?

时间:2018-01-11 20:12:37

标签: javascript

我认为这是一个我在某处遗漏的小错误。代码顶部顶部的var JSONurl应由changecoin()函数更改。但是,当网页调用changecoin()时,函数会执行,但JSONurl字符串不会更改并保持其初始值。

/*jslint plusplus: true */
//console.log("javascript is working");
"use strict";

var lastPrice;
var initSet = 0;
var JSONurl = "http://coincap.io/page/XRP";

document.getElementById("dropdown-btc").addEventListener("click", function(){
    changeCoin("btc");
});

document.getElementById("dropdown-eth").addEventListener("click", function(){
    changeCoin("eth");
});

document.getElementById("dropdown-xrp").addEventListener("click", function(){
    changeCoin("xrp");
});



$( document ).ready(function () {
    console.log( "ready!" );
    getRippleVals(JSONurl);
    //setInterval(getRippleVals(JSONurl), 5000);

});


function getRippleVals(JSONurl) {
    console.log("");
    var url = JSONurl;
    console.log("JSON URL: " + url);
    console.log("Gloval JSON URL: " + JSONurl);
    $.getJSON(url,
            function (data) {

            //function commands, not relevant

          });
          setTimeout(getRippleVals, 5000, JSONurl);

}



function bFormatter(num) {
    return num > 999999999 ? (num/1000000000).toFixed(2) + 'Bn' : num
}

function changeCoin(coin)
{
  console.log(coin);
  var coin = coin.toUpperCase();
  $("#dropdownMenuButton").html(coin);

  if(coin === "BTC")
  {
    JSONurl = "http://coincap.io/page/BTC";
    $("#pairing-text").html("BTC : USD");
    lastPrice = 0;
    console.log("JSONurl set to: " + "http://coincap.io/page/BTC");
  }
  else if(coin === "ETH")
  {
    JSONurl = "http://coincap.io/page/ETH";
    $("#pairing-text").html("ETH : USD");
    lastPrice = 0;
    console.log("JSONurl set to: " + "http://coincap.io/page/ETH");
  }
  else if(coin === "XRP")
  {
    JSONurl = "http://coincap.io/page/XRP";
    $("#pairing-text").html("XRP : USD");
    lastPrice = 0;
    console.log("JSONurl set to: " + "http://coincap.io/page/XRP");
  }

}

1 个答案:

答案 0 :(得分:0)

问题是你在getRippleVals调用的setTimeout,你继续传入初始的JSONurl,所以它总是你第一次设置它。

看起来您尝试使用setInterval(这是重复该函数的正确方法),您只需要使用匿名函数将参数(JSONurl)传递给getRippleVals



"use strict";

var lastPrice;
var initSet = 0;
var JSONurl = "http://coincap.io/page/XRP";

document.getElementById("dropdown-btc").addEventListener("click", function() {
  changeCoin("btc");
});

document.getElementById("dropdown-eth").addEventListener("click", function() {
  changeCoin("eth");
});

document.getElementById("dropdown-xrp").addEventListener("click", function() {
  changeCoin("xrp");
});

$(document).ready(function() {
  console.log("ready!");
  getRippleVals(JSONurl);
  //** use setInterval like below
  setInterval(function() {
    getRippleVals(JSONurl)
  }, 5000);

});

function getRippleVals(JSONurl) {
  console.log("");
  var url = JSONurl;
  //console.log("JSON URL: " + url);
  console.log("Global JSON URL: " + JSONurl);
  $.getJSON(url, function(data) {
    //function commands, not relevant
  });
  // take out this setTimeout
  //setTimeout(getRippleVals, 5000, JSONurl);

}

function changeCoin(coin) {
  console.log(coin);
  var coin = coin.toUpperCase();
  $("#dropdownMenuButton").html(coin);

  if (coin === "BTC") {
    JSONurl = "http://coincap.io/page/BTC";
    $("#pairing-text").html("BTC : USD");
    lastPrice = 0;
    console.log("JSONurl set to: " + "http://coincap.io/page/BTC");
  } else if (coin === "ETH") {
    JSONurl = "http://coincap.io/page/ETH";
    $("#pairing-text").html("ETH : USD");
    lastPrice = 0;
    console.log("JSONurl set to: " + "http://coincap.io/page/ETH");
  } else if (coin === "XRP") {
    JSONurl = "http://coincap.io/page/XRP";
    $("#pairing-text").html("XRP : USD");
    lastPrice = 0;
    console.log("JSONurl set to: " + "http://coincap.io/page/XRP");
  }

}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="dropdown-btc">BTC</button>
<button id="dropdown-eth">ETH</button>
<button id="dropdown-xrp">XRP</button>
&#13;
&#13;
&#13;