简体   繁体   English

如何更改网址参数?

[英]How to change url parameter?

How can I change the parameter "symbol" in the url to each element of the array one by one and run the function multiple times? 如何才能将网址中的参数“符号”一一更改为数组的每个元素,然后多次运行该函数?

var symbols = [MSFT, CSCO, FB, AMZN, GOOG];

window.onload = function() {
$.ajax({
    type: "GET",
    url: "https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=AAPL&interval=1min&apikey=T6UEJETEQRVGDJS9",
    success: function(result){
        stocks = result;
        document.getElementById("myDiv").innerHTML = stocks;

    }
 });
}

Thanks! 谢谢!

You have to separate the AJAX call to stand alone function, and then call the that function each time with different parameter. 您必须将AJAX调用分离为独立的函数,然后每次使用不同的参数来调用该函数。

Like so: 像这样:

window.onload = function() {
    var symbols = ['MSFT', 'CSCO', 'FB', 'AMZN', 'GOOG'];

    symbols.forEach( symbol => makeAjaxCall(symbol));

}

function makeAjaxCall(param){

$.ajax({
    type: "GET",
    url: "https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol="+ param +"&interval=1min&apikey=T6UEJETEQRVGDJS9",
    success: function(result){
        stocks = result;
        document.getElementById("myDiv").innerHTML = stocks;
    }
 });
}

Do it in a forEach on the array: 在数组的forEach中执行此操作:

var symbols = ["MSFT", "CSCO", "FB", "AMZN", "GOOG"];

window.onload = function() {
    symbols.forEach(function(sym){
        var url = "https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&" + sym + "=AAPL&interval=1min&apikey=T6UEJETEQRVGDJS9";
        $.ajax({
            type: "GET",
            url: url,
            success: function(result){
                stocks = result;
                document.getElementById("myDiv").innerHTML = stocks;

            }
         });
    }   
})

I didn't include promises to keep it simple, but you could use one more parameter to specify the DOM element that will receive the value of the stock. 我没有包含使它保持简单的承诺,但是您可以使用另一个参数来指定将接收股票价值的DOM元素。

window.onload = function() {
    var symbols = ['MSFT', 'CSCO', 'FB', 'AMZN', 'GOOG'];
    symbols.forEach(symbol => getSymbol(symbol, divId));
}

function getSymbol(param, divId){
  $.ajax({
      type: "GET",
      url: "https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol="+ param +"&interval=1min&apikey=T6UEJETEQRVGDJS9",
      success: function(result){
          stocks = result;
          document.getElementById(divId).innerHTML = stocks;
      }
   });
}

You can set it as a variable: 您可以将其设置为变量:

$  node
> var symbol = 'aallalla'
undefined
> var html = 'http://www.google.com/?symbol='
undefined
> html + symbol
'http://www.google.com/?symbol=aallalla'
> 

This will allow you to change it on demand. 这将允许您按需更改它。

const getSymbol = (symbol) => {
  return new Promise((resolve, reject) => {
    $.ajax({
        type: "GET",
        url: `https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=${symbol}&interval=1min&apikey=T6UEJETEQRVGDJS9`,
        success: function(result){
            resolve(result);
        }
    });
  });
};

window.onload = function() {
  const symbolPromises = symbols.map((symbol) => {
    return getSymbol(symbol);
  });

  Promise.all(symbolPromises).then((arrayOfResult) => {
    const stocks = result.join('</br>');
    document.getElementById("myDiv").innerHTML = stocks;
  }).catch((err) => {
    console.error(err);
  });       
}

Following this approach you can call as much symbols as you need having all symbol results in the result gave in promise.all function and just is necessary present this result to the user. 按照这种方法,您可以根据需要调用尽可能多的符号,并将所有符号结果存入Promise.all函数,并将结果呈现给用户即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM