简体   繁体   English

将值/变量传递到异步 function

[英]Pass a value/variable into an async function

async function getChampionID(randomChampionID) {
    let response = await fetch('http://ddragon.leagueoflegends.com/cdn/11.10.1/data/en_US/champion.json');
    let body = await response.json();
    var championData = Object.keys(body.data)
    var randomChampionKey = Math.floor(Math.random() * (154 - 0 + 1) + 0)
    return randomChampionID = championData[randomChampionKey]
}

async function getChampionName(randomChampionName) {
    let result = await getChampionID();
    let response = await fetch(`http://ddragon.leagueoflegends.com/cdn/11.10.1/data/en_US/champion/${result}.json`)
    let body = await response.json();
    return randomChampionName = body.data[result].name
}


var randomChampion = document.getElementById('random-champion')
var bgimg = document.getElementById('background-image')
var championSquare = document.getElementById('square')

randomChampion.addEventListener("click", async () =>
{
    let championID = await getChampionID()
    let championName = await getChampionName()
    bgimg.style.backgroundImage = `url('http://ddragon.leagueoflegends.com/cdn/img/champion/splash/${championID}_0.jpg')`
    championSquare.src=`http://ddragon.leagueoflegends.com/cdn/11.11.1/img/champion/${championID}.png`
    console.log(championID)
    console.log(championName)
})

The getChampionName() function takes a random value from getChampionID , so whenever I call both of them through a button event listener, the getChampionID() generates a random ID (#1), the getChampionName() once again takes another value from getChampionID() , result in a different ID (#2), but I need the getChampionName() to take the #1 ID getChampionName() function 从getChampionID获取一个随机值,所以每当我通过按钮事件侦听器调用它们时, getChampionID()生成一个随机 ID (#1), getChampionName()再次从getChampionID() ,导致不同的 ID (#2),但我需要getChampionName()来获取 #1 ID

Firstly, the functions are not being passed a parameter and therefore do not need anything within the parentheses when being defined.首先,函数没有被传递参数,因此在定义时不需要括号内的任何内容。

Secondly, within the event listener, I would simply call a function called getChampion that gets a random ID, then gets the details, and returns both the champion detail and the ID for further use as an object.其次,在事件监听器中,我会简单地调用一个名为getChampion的 function,它会获取一个随机 ID,然后获取详细信息,并返回冠军详细信息和 ID 以供进一步用作 object。

My code would look like this.我的代码看起来像这样。

async function getChampionID() {
    let response = await fetch('http://ddragon.leagueoflegends.com/cdn/11.10.1/data/en_US/champion.json');
    let body = await response.json();
    var championData = Object.keys(body.data)
    var randomChampionKey = Math.floor(Math.random() * (154 - 0 + 1) + 0)
    return championData[randomChampionKey]
}

async function getChampion() {
    let result = await getChampionID();
    let response = await fetch(`http://ddragon.leagueoflegends.com/cdn/11.10.1/data/en_US/champion/${result}.json`)
    let body = await response.json();
    return { name: body.data[result].name, id: result }
}


var randomChampion = document.getElementById('random-champion')
var bgimg = document.getElementById('background-image')
var championSquare = document.getElementById('square')

randomChampion.addEventListener("click", async () =>
{
    let champion = await getChampion()
    bgimg.style.backgroundImage = `url('http://ddragon.leagueoflegends.com/cdn/img/champion/splash/${champion.id}_0.jpg')`
    championSquare.src=`http://ddragon.leagueoflegends.com/cdn/11.11.1/img/champion/${champion.id}.png`
    console.log(champion.id)
    console.log(champion.name)
})

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

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