[英]a function that worked is suddenly “not a function” while setInterval() is running a different function
tl;dr: my function doesn't run while I've got setInterval() running another function every 3 seconds. tl; dr:我的setInterval()每3秒运行另一个函数时,我的函数没有运行。
I'm making a text-based gardening game that runs "plant()" when I type plant. 我正在制作一个基于文本的园艺游戏,当我键入植物时,该游戏将运行“ plant()”。 I've also got a setInterval(updatePlots, 3000) going.
我还有一个setInterval(updatePlots,3000)。
both of these functions work fine on their own, but when I try to run plant() while setInterval() is going, it brings up the error Uncaught TypeError: plant is not a function
这两个函数都可以正常工作,但是当我尝试在setInterval()进行时运行plant()时,它会
Uncaught TypeError: plant is not a function
错误Uncaught TypeError: plant is not a function
(I know it's the setInterval() because I tested planting without it running, and it worked fine.) (我知道这是setInterval(),因为我在不运行种植的情况下对其进行了测试,并且效果很好。)
what I tried (didn't work): 我尝试了什么(没用):
if (command == "plant") {
clearInterval(timer);
plant(a, b);
var timer = setInterval(updatePlots, 3000);
}
I'm not really sure what code I've got to show, since it seems more of a fundamental problem than single-line error... but here it is. 我不太确定要显示什么代码,因为它看起来比单行错误更像是一个基本问题……但是在这里。
function updatePlots() {
fullplots = [];
for (i = 0; i < plots.length; i++) {
if (plots[i].length) {
fullplots.push(i);
}
}
for (i = 0; i < fullplots.length; i++) {
plant = plots[fullplots[i]][0];
status = plots[fullplots[i]][1];
growth = plots[fullplots[i]][2];
if (growth < 100) {
plots[fullplots[i]][2]++;
}
}
if (document.getElementById('plots').style.display == 'block') {
getPlots();
}
}
...
function processTwo(command, a) {
if (command == 'plant') {
clearInterval(timer);
console.log('about to plant 1'+a);
plant(1, a);
var timer = setInterval(updatePlots, 3000);
}
else { createError() }
}
update: solved! 更新:解决了!
function updatePlots() {
// this way, the global plant function is not overwritten
// @see JavaScript variable scope
var plant;
// You might want to 'var' your other local (?) variables, too
// var fullplots, i, status, growth;
fullplots = [];
for (i = 0; i < plots.length; i++) { //get fullplots
if (plots[i].length) {
fullplots.push(i);
}
}
for (i = 0; i < fullplots.length; i++) {
// at this line of code you overwrite the global plant, which is not a function anymore then
plant = plots[fullplots[i]][0];
status = plots[fullplots[i]][1];
growth = plots[fullplots[i]][2];
if (growth < 100) { //increment
plots[fullplots[i]][2]++;
}
}
if (document.getElementById('plots').style.display == 'block') {
getPlots();
}
}
The problem arises in the updatePlots method called by setInterval. 该问题出现在setInterval调用的updatePlots方法中。 What you're doing in there is assigning a new value to "plant"
您在其中所做的就是为“植物”分配新值
plant = plots[fullplots[i]][0];
Then, when plant is called, it points to the new value you assigned to it in updatePlots instead of the function you originally had. 然后,在调用plant时,它指向您在updatePlots中为其分配的新值,而不是您最初拥有的函数。 You have to declare a new variable in updatePlots to avoid changing the plant method.
您必须在updatePlots中声明一个新变量,以避免更改plant方法。 I'd use a different name to avoid confusion.
为了避免混淆,我会使用其他名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.