如何在Javascript中传递对$(function())的引用

如何在Javascript中传递对$(function())的引用,javascript,Javascript,在我的JS文件中,我有一个名为getRippleVals()的函数,它通过url调用JSON文件,该函数在页面加载后每5秒执行一次,该函数由$(document).ready(function(){})调用。我还有一个带有onclick功能的按钮,名为changecoin(coin) 我想做的是,当单击按钮时,它调用getRippleVals(),并传递一个字符串参数来更改它调用的JSON url。然后,单击按钮后对getRippleVals()的每次调用都使用新的URL 目前,我使用了一个名为

在我的JS文件中,我有一个名为
getRippleVals()
的函数,它通过url调用JSON文件,该函数在页面加载后每5秒执行一次,该函数由
$(document).ready(function(){})
调用。我还有一个带有onclick功能的按钮,名为
changecoin(coin)

我想做的是,当单击按钮时,它调用
getRippleVals()
,并传递一个字符串参数来更改它调用的JSON url。然后,单击按钮后对getRippleVals()的每次调用都使用新的URL

目前,我使用了一个名为
JSONurl
的全局变量,函数使用该变量查找JSON文件。单击按钮时,
JSONurl
的值将更改。但是,目前这并没有立即调用
getRippleVals()
函数,这意味着在页面更新之前,我必须等待几秒钟才能再次调用该函数

我不知道该如何做,因为我不知道如何将参数传递给由
$(document.ready(function(){})

这是我的JS:

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!" );


  $(function getRippleVals() {
      console.log("");
      $.getJSON(JSONurl,
              function (data) {

              //... extra function methods are here, not relevant to the problem.


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



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;
  }
  else if(coin === "ETH")
  {
    JSONurl = "http://coincap.io/page/ETH";
    $("#pairing-text").html("ETH : USD");
    lastPrice = 0;
  }
  else if(coin === "XRP")
  {
    JSONurl = "http://coincap.io/page/XRP";
    $("#pairing-text").html("XRP : USD");
    lastPrice = 0;
  }

}

为什么不这样做呢

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!" );
    var JSONurl = "http://coincap.io/page/XRP"
    //call getRippleVals() and pass the JSONurl, it will execute after 5 sec
    setTimeout(getRippleVals(JSONurl), 5000);
});


function getRippleVals(JSONurl){
    $.getJSON(JSONurl,
              function (data) {

              //... extra function methods are here, not relevant to the problem.


          });
      });
}



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

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

}
您必须等待5秒才能执行
getRippleVals()
,因为您在
getRippleVals()中调用了
setTimeout

编辑

很抱歉稍后回复。至于你的要求,我已经在下面做了一个示例代码

var JSONurl = "http://coincap.io/page/XRP";
var counter = 5000;
var timer = null;
$(document).ready(function(){
      getRippleValstimer(); //call the timer so getRippleVals() will execute every 5sec
        //will execute 5 sec after page load
        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");
      });
});

//the timer function
function getRippleValstimer(){
      timer = setTimeout(function(){
        getRippleValstimer(); getRippleVals();
      },counter);
}

function getRippleVals(){
  //your code here
  console.log(JSONurl);
}


function changeCoin(coin){
  var coin = coin.toUpperCase();
  console.log(coin);
  if(coin === "BTC")
  {
    JSONurl = "http://coincap.io/page/BTC";
    clearTimeout(timer); //clear the timer
    getRippleVals();//call getRippleVals() so it will execute without waiting for 5 seconds
    getRippleValstimer(); //initialize the timer function again

  }
  else if(coin === "ETH")
  {
    JSONurl = "http://coincap.io/page/ETH";
    clearTimeout(timer); //clear the timer
    getRippleVals();//call getRippleVals() so it will execute without waiting for 5 seconds
    getRippleValstimer(); //initialize the timer function again
  }
  else if(coin === "XRP")
  {
    JSONurl = "http://coincap.io/page/XRP";
    clearTimeout(timer); //clear the timer
    getRippleVals();//call getRippleVals() so it will execute without waiting for 5 seconds
    getRippleValstimer(); //initialize the timer function again
  }

}

我现在将代码更改为:我希望在页面加载时调用该函数一次,然后每隔5秒调用一次。这段代码正在页面加载时调用函数,但它不是每5秒刷新一次,为什么没有发生这种情况?很抱歉稍后回复。我刚刚更新了我的答案。请检查一下。