简体   繁体   中英

“setTimeout is not defined” when using setTimeout?

I get a "setTimeout is not defined" error returned whenever I run this code, whats wrong with it? I know its probably a stupid mistake i'm making... the npc. are specific to the program im running this code in, the script itself is separate from any html code

var time = Math.floor((world.getTime() % 24000) / 10);
npc.say(time);
if (time == 1195) {
  npc.say("I need some rest");
  //npc.navigateTo(-724,60,782,1.0);
  npc.setHome(-724,60,782);
  npc.setAnimation(2);
}
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){
      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); 
      }
  }
}

Your while loop looks like it's causing a problem. It's stuck in an infinite loop.

Try removing the while loop for debugging and see if your setTimeout error still happens:

example:

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); 
     // }
  }
}

If that fixes the problem, you'll want to consider how to update the time inside of the while loop, so eventually you'll break out of it.

I made a sample jsfiddle for you to try http://jsfiddle.net/qanzdm8g/7/ . This should allow you to try different things and see what works!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM