简体   繁体   English

我需要帮助在Firefox的开发人员控制台中创建循环

[英]I need help creating a loop in dev console in firefox

I am trying to use the dev console to give mario a mushroom every 5 seconds (in the browser game super mario html5) 我正在尝试使用开发控制台每5秒给马里奥一个蘑菇(在浏览器游戏中,超级马里奥html5)

I can give mario mushrooms manually by typing marioShroons(mario) but I would like to have it on loop so I don't have to pause the game every time I want a mushroom. 我可以通过键入marioShroons(mario)手动给马里奥蘑菇加油,但我希望循环播放它,这样我就不必每次都想要蘑菇时暂停游戏。 I have tried a while loop and set timeout but I can't figure it out. 我尝试了一个while循环并设置了超时时间,但是我无法弄清楚。 The only coding languages I familiar with are c++ and html. 我唯一熟悉的编码语言是c ++和html。 ** **

while(data.time.amount > 0) {
  killOtherCharacters()
}

setTimeout(function() {
  killOtherCharacters()
}, 1000);

I expected these lines of code to not give me a mushroom, but to automatically kill enemies. 我希望这些代码行不会给我带来麻烦,而是会自动杀死敌人。 But on the first try (the while loop) it froze the tab and I had to reload the page. 但是在第一次尝试(while循环)时,它冻结了选项卡,我不得不重新加载页面。

With the set timeout, it didn't make any obvious results, it killed all near characters once and then stopped. 设置超时后,它没有产生任何明显的结果,它杀死了所有附近的角色,然后停止了。

The reason your while loop froze the page is because Javascript can only do one thing at a time and you told it to always run your while function, blocking all other Javascript from running on your site. while循环冻结页面的原因是Javascript一次只能做一件事,并且您告诉它始终运行while函数,从而阻止所有其他Javascript在您的站点上运行。

setTimeout is only run once after a set time ( see documentation ), if you want to run something every x miliseconds it's better to use setInterval instead . setTimeout在设置的时间后仅运行一次( 请参阅文档 ),如果您想每隔x​​毫秒运行一次,最好改用setInterval

var intervalID = window.setInterval(killOtherCharacters(), 500); //run this every 500 ms

Use setInterval if you want killOtherCharacters() to be called repeatedly. 如果要重复调用killOtherCharacters(),请使用setInterval。

const interval = setInterval(function() {killOtherCharacters() },1000);

Then when you want the function to stop being called: 然后,当您希望函数停止被调用时:

clearInterval(interval);

You tried using setTimeout , and it only worked once. 您尝试使用setTimeout ,它只能工作一次。 This is to be expected, because: 这是可以预期的,因为:

Window.setTimeout() sets a timer which executes a function or specified piece of code once the timer expires Window.setTimeout()设置一个计时器,一旦计时器到期,该计时器将执行功能或指定的代码

From MDN MDN

What you need to do is use setInterval : 您需要做的是使用setInterval

The setInterval() method...repeatedly calls a function or executes a code snippet, with a fixed time delay between each call. setInterval()方法...重复调用函数或执行代码段,每次调用之间有固定的时间延迟。

From MDN MDN

So in your console, you should write this: 因此,在控制台中,您应该这样编写:

setInterval(killOtherCharacters, 1000);

(I removed the anonymous function because it wasn't needed - you only need an anonymous function if you're passing parameters or doing multiple things. You do need to remove the () for this though). (我删除了匿名函数,因为它是不需要的-仅在传递参数或执行多项操作时才需要匿名函数。不过,您确实需要为此删除() )。

And if you want to stop the function from executing, assign a variable to the interval: 并且,如果要停止执行该函数,请为该间隔分配一个变量:

var killCharacters = setInterval(killOtherCharacters, 1000);

Then call clearInterval upon this variable to clear the interval (stop the loop): 然后对此变量调用clearInterval以清除间隔(停止循环):

clearInterval(killCharacters);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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