Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将result.js传递给另一个.js_Javascript_Html - Fatal编程技术网

Javascript 将result.js传递给另一个.js

Javascript 将result.js传递给另一个.js,javascript,html,Javascript,Html,我想将结果从var url(index.js)传递到game.js,以便它遵循我需要的过程。可能吗?任何帮助都将不胜感激,非常感谢。 我编写了完整的index.js代码,以便您了解想要实现的目标。 我尝试过很多方法,但都没有成功,我不知道还有什么可以尝试 index.js game.js 要导出值,它必须是exported。您可以使用module.exports={url:'localhost:3000'}或export-const-url=https://stackoverflow.com“语

我想将结果从var url(index.js)传递到game.js,以便它遵循我需要的过程。可能吗?任何帮助都将不胜感激,非常感谢。 我编写了完整的index.js代码,以便您了解想要实现的目标。 我尝试过很多方法,但都没有成功,我不知道还有什么可以尝试

index.js

game.js


要导出值,它必须是
export
ed。您可以使用
module.exports={url:'localhost:3000'}
export-const-url=https://stackoverflow.com“
语法取决于您的项目工具See:可能是一个tad原语,但是您也可以将变量放入w/o
export
--
export
是更好的练习路线。如前所述,
newUrl
仅在事件监听器的功能范围内定义,但[btnFetch.addEventListener('click',function(e){console.log(newUrl);});]不是一个函数:c。。。我不明白这是怎么做的
      //SELECT CATEGORY
//CATEGORY => 8, 9, 10, 11, ..., 32

const obj = {
    url: 'https://opentdb.com/api.php?amount=20',
    category: '',
    difficulty: ''
}

let newUrl = Object.values(obj).join('');

function selectCat() {
    var c = document.getElementById('cat').value;
    console.log(c);
    obj.category = c;
    newUrl = Object.values(obj).join('');
}

//SELECT DIFFICULTY
//DIFFICULTY => any, easy, medium, hard
function selectDiff() {
    var d = document.getElementById('diff').value;
    console.log(d);
    obj.difficulty = d;
    newUrl = Object.values(obj).join('');
}


  /*NEST VALUE TO CATEGORY AND DIFFICULTY
    NEXT TO THE URL TO GET THE NEWURL*/

  //CLICK EVENT BUTTON


  var btnFetch = document.getElementById('fetch');

  /*var url =*/ btnFetch.addEventListener('click', function(e) {
    console.log(newUrl);});
fetch(
    url
    )
    .then(res => {
      return res.json();
    })
    .then(loadedQuestions => {
      console.log(loadedQuestions.results);
      questions = loadedQuestions.results.map(loadedQuestion => {
        const formattedQuestion = {
          question: loadedQuestion.question
        };

        const answerChoices = [...loadedQuestion.incorrect_answers];
        formattedQuestion.answer = Math.floor(Math.random() * 3) + 1;
        answerChoices.splice(
        formattedQuestion.answer - 1,
        0,
        loadedQuestion.correct_answer
        );
        answerChoices.forEach((choice, index) => {
          formattedQuestion["choice" + (index + 1)] = choice;
        });

        return formattedQuestion;
      });

      startGame();
  })
  .catch(err => {
    console.error(err);
  });