用于循环的 Java 脚本

我希望循环运行直到提示的值,但它会无限。需要帮忙。这是我的 javascript 代码: 我的代码

var round = prompt("enter text");

var roundno = round;


var images_arr = ["../img/paper.png", "../img/stone.png", "../img/sisor.png"];

var size = images_arr.length

function myFunction() {

for (var i = 0; i = roundno; i + 1) {

    console.log(i);

    setInterval(function () {

        var x = Math.floor(size * Math.random())

        $('#random').attr('src', images_arr[x]);

    }, 1500);


    setInterval(function () {

        var sound = new Audio("../audio/audio.mp3");

        sound.play();

    }, 3000);


    if (i = roundno) {

        break;

     }

    }

  }


慕标琳琳
浏览 165回答 2
2回答

长风秋雁

你的代码有很多问题。var round = prompt("enter text"); // how do you know the user enters a number?var defaultNumberOfRounds = 1;&nbsp; // I added this row// CHANGED THIS IN EDIT&nbsp;var roundno = round; // should be: isNaN(Number(round)) ? defaultNumberOfRounds : roundvar images_arr = ["../img/paper.png", "../img/stone.png", "../img/sisor.png"];var size = images_arr.lengthfunction myFunction() {// CHANGED THIS IN EDIT the conditions within () should be: var i = 0; i < roundno; i++for (var i = 0; i = roundno; i + 1) {&nbsp; &nbsp; console.log(i);&nbsp; &nbsp; // all iterations in the loop will execute this at the same time.&nbsp; &nbsp; setInterval(function () {&nbsp; &nbsp; &nbsp; &nbsp; var x = Math.floor(size * Math.random())&nbsp; &nbsp; &nbsp; &nbsp; $('#random').attr('src', images_arr[x]); // JQuery&nbsp; &nbsp; }, 1500);&nbsp; &nbsp; // all iterations in the loop will execute this at the same time.&nbsp; &nbsp; setInterval(function () {&nbsp; &nbsp; &nbsp; &nbsp; var sound = new Audio("../audio/audio.mp3");&nbsp; &nbsp; &nbsp; &nbsp; sound.play();&nbsp; &nbsp; }, 3000);&nbsp; &nbsp; if (i = roundno) { // should be i == roundno&nbsp; &nbsp; &nbsp; &nbsp; break; // don't need to break it, because your for loop's condition should take care of this&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; }&nbsp; }} // ADDED THIS IN EDIT: missing curly bracket[编辑] 我添加了一个片段来显示我的代码正在运行。我注释了 for 循环中的所有代码,我不得不更改roundnofor 循环中的声明和语句。var round = prompt("enter text");var defaultNumberOfRounds = 1;var roundno = isNaN(Number(round)) ? defaultNumberOfRounds : round;var images_arr = ["../img/paper.png", "../img/stone.png", "../img/sisor.png"];var size = images_arr.length;console.log(`round: ${round}, roundno: ${roundno}`);function myFunction() {&nbsp; for (var i = 0; i < roundno; i++) {&nbsp; &nbsp; console.log(i);&nbsp; &nbsp; /*setInterval(function () {&nbsp; &nbsp; &nbsp; &nbsp; var x = Math.floor(size * Math.random())&nbsp; &nbsp; &nbsp; &nbsp; $('#random').attr('src', images_arr[x]); // JQuery&nbsp; &nbsp; }, 1500);&nbsp; &nbsp; setInterval(function () {&nbsp; &nbsp; &nbsp; &nbsp; var sound = new Audio("../audio/audio.mp3");&nbsp; &nbsp; &nbsp; &nbsp; sound.play();&nbsp; &nbsp; }, 3000);*/&nbsp; }}myFunction();

有只小跳蛙

你没有增加i改变i&nbsp;+&nbsp;1到以下之一:i++i += 1i = i + 1您还需要if在循环结束时修复语句。您要检查是否i等于roundno。而不是使用赋值运算符来分配to=的值,使用or进行相等性检查。roundnoi=====if&nbsp;(i&nbsp;==&nbsp;roundno)&nbsp;{&nbsp; &nbsp;&nbsp;break; &nbsp;&nbsp;}您在循环条件中也犯了同样的错字。将循环条件更改为i == roundno并删除if循环结束时的语句,因为循环条件固定,if循环内的语句是不必要的。for&nbsp;(var&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;==&nbsp;roundno;&nbsp;i++)&nbsp;{&nbsp; &nbsp;&nbsp;//&nbsp;code &nbsp;&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript