我的脚本导致浏览器冻结并要求我停止脚本。使用firebug我可以看到for循环无休止地循环而没有取得任何进展。这是循环:
for (var x = 1; x < 7; x++) {
var y = x; //to stop the value of x being altered in the concat further down
var questionidd = "mcq_question_id";
console.log("1 = " + questionidd);
var questionid = questionidd.concat(y); // mcq_question_id$ctr the question number
console.log("2 = " + questionid);
var mcqid = form[questionid].value; // the questions id on db
console.log("3 = " + mcqid);
var answerr = "mcq_question";
var answer = answerr.concat(y); // mcq_question$ctr the questions chosen answer
var chosenanswer = form[answer].value; // the answers value
console.log("4 = " + chosenanswer);
var amp = "&";
var equal = "=";
var questionide = questionid.concat(equal); // "mcq_question_id$ctr="
var questionida = amp.concat(questionide); // "&mcq_question_id$ctr="
var answere = amp.concat(answer, equal); // "&mcq_question$ctr="
if (x = 1) {
send.push(questionide, mcqid, answere, chosenanswer);
}
else {
send.push(questionida, mcqid, answere, chosenanswer);
}
}
更新 - 修正了!愚蠢的错误是最糟糕的
答案 0 :(得分:6)
if (x = 1) {
应该是
if (x === 1) {
===
运算符比较,而赋值运算符=
分配。很多人犯了这个错误。 :)
当第一个循环运行时,它将x
设置为零,并且无限地执行,直到进程终止。这就是循环不会停止的原因。
答案 1 :(得分:5)
if (x = 1) {
应为if (x === 1) {
考虑切换到能够捕获此类简单编程错误的IDE。
答案 2 :(得分:2)
看起来你应该有“if(x == 1)”而不是“if(x = 1)”。
您的代码重复将x设置为值1,而不是检查它是否等于1。