使用setTimeout时“setTimeout未定义”?

时间:2015-10-03 14:54:29

标签: javascript

每当我运行此代码时,我都会收到“setTimeout is not defined”错误,这是错误的吗?我知道这可能是我犯的一个愚蠢的错误...... npc。特定于运行此代码的程序,脚本本身与任何HTML代码

分开
covr

2 个答案:

答案 0 :(得分:-1)

不要在循环中创建函数。请使用var interval = setInterval(function..作为循环,并使用clearInterval(interval)停止。

答案 1 :(得分:-1)

你的while循环看起来像是导致问题。它陷入无限循环。

尝试删除while循环以进行调试,并查看是否仍然发生setTimeout错误:

示例:

else if (time == 2385) {
  npc.setAnimation(0);
  //npc.navigateTo(-734,68,769,1.0);
  npc.setHome(-734,68,769);
  npc.say("Time to tend the crops");
  // while (time != 1194){ -- commenting out for debugging!
      npc.say(time);
      var randum = Math.floor((Math.random() * 7) + 1);
      if (randum == 1) {
          setTimeout(function() {
              npc.say("1");
              npc.setHome(-734,67,768);
          },5000); 
     // }
  }
}

如果这样可以解决问题,那么您将考虑如何更新while循环内部的时间,以便最终打破它。

我为你做了一个示例jsfiddle http://jsfiddle.net/qanzdm8g/7/。这应该允许你尝试不同的东西,看看有什么作用!

相关问题