猿问

使用 return 时 JavaScript 多次返回

我正在尝试接收多个 JSON 文件的数据。我有我需要的数据,但是多次,即使我使用了 return 语句。我需要getData()函数中的数据。这需要进展到loadImage()功能。我不知道为什么代码多次返回相同的内容。

提前致谢。

我的 Chrome 开发者控制台:

我的目录结构:

http://img2.mukewang.com/61d839e50001530702570725.jpg

JSON文件结构:


{

    "filename": "legs",

    "fileExtension": "png",

    "changesIfJumping": true,

    "customizeable": true

}


紫衣仙女
浏览 236回答 1
1回答

陪伴而非守候

使用更现代的方式-获取,而不是XHRasync function getData(characterPart) {  try {    let response = await fetch(`./character/${characterPart}/data.json`);    return response.json()  } catch (e) {    throw new Error(`Could not get file with the name "data.json" in the directory "${characterPart}".`);  }}// TESTasync function start() {  window.defaultData = await getData('xxx');}start();
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答