JavaScript跟踪全局变量?

时间:2022-06-16 16:49:50

Im trying to use the questionID variable to keep track of some questions and answers. But it seems I have problem with that the variable lose its memory, for each time I run my function ? thats what I think anyway.

我试图使用questionID变量来跟踪一些问题和答案。但是,每次运行我的函数时,似乎我都有变量丢失内存的问题?这就是我的想法。

When i ask for: color, and then follow up with an answer of: pink, purple or green, it jumps to the last statement; else if(questionID == -1) which is not my intention.

当我要求:颜色,然后跟进答案:粉红色,紫色或绿色,它跳到最后一个声明;否则if(questionID == -1)这不是我的意图。

How can I fix this behaviour ? I run the script locally.

我该如何解决这个问题?我在本地运行脚本。

var questionID = -1;

function askQ() {
    var findme = document.getElementById('askBox').value.toLowerCase();
    var dontUnderstand = new Array();
    dontUnderstand[0] = "Excuse me, What did You say ?";
    dontUnderstand[1] = "Sorry, I cant understand You now. ^_^";
    dontUnderstand[2] = "Sorry!, I got to go now... CU !";
    dontUnderstand[3] = "ohh... Can you repeat that one more time ?";
    var randomIndex = Math.round(Math.random() * 3);
    if (findme.match(/(age|old)/)) {
        document.getElementById('bubble').innerHTML = "ohh,, I am " +
            obj1.age + " years old, How old are You ?";
    }
    if (!isNaN(findme)) {
        document.getElementById('bubble').innerHTML = "ohh.. that's kinda old !";
    }
    if (findme.match(/(color)/)) {
        document.getElementById('bubble').innerHTML = "My favourite color " +
            " is Purple, What's yours ?";
        questionID = 6;
    }
    if (findme.match(/(pink|purple|green)/) && questionID == 6) {
        document.getElementById('bubble').innerHTML = "well...that's cute !!";
        questionID = -1;
    }
    if (findme.match(/(hi|hello)/)) {
        document.getElementById('bubble').innerHTML = "Hi there, nice to meet You !";
    } else if (questionID == -1) {
        document.getElementById('bubble').innerHTML = dontUnderstand[randomIndex];
    }
}

2 个解决方案

#1


0  

 if(findme.match(/(pink|purple|green)/) && questionID == 6){
              document.getElementById('bubble').innerHTML = "well...that's cute !!";
      // You're setting it to -1.
      questionID = -1;
  }

Also, why not limit your pollution of the global object by making questionID a property of the askQ function.

此外,为什么不通过使questionID成为askQ函数的属性来限制对全局对象的污染。

askQ.questionID = 6

askQ.questionID = 6

#2


1  

I'd start by addint this to the top and bottom of your function:

我首先将addint添加到函数的顶部和底部:

console.log('starting', questionID);


console.log('ending', questionID);

Then use your debug log to see what is going on... or you could add a watch and step through your code using firebug or chrome

然后使用调试日志查看正在发生的事情......或者您可以使用firebug或chrome添加监视并逐步执行代码

Also wondering why you are not using jQuery...

还想知道你为什么不使用jQuery ...

#1


0  

 if(findme.match(/(pink|purple|green)/) && questionID == 6){
              document.getElementById('bubble').innerHTML = "well...that's cute !!";
      // You're setting it to -1.
      questionID = -1;
  }

Also, why not limit your pollution of the global object by making questionID a property of the askQ function.

此外,为什么不通过使questionID成为askQ函数的属性来限制对全局对象的污染。

askQ.questionID = 6

askQ.questionID = 6

#2


1  

I'd start by addint this to the top and bottom of your function:

我首先将addint添加到函数的顶部和底部:

console.log('starting', questionID);


console.log('ending', questionID);

Then use your debug log to see what is going on... or you could add a watch and step through your code using firebug or chrome

然后使用调试日志查看正在发生的事情......或者您可以使用firebug或chrome添加监视并逐步执行代码

Also wondering why you are not using jQuery...

还想知道你为什么不使用jQuery ...