简体   繁体   English

如何在代码外部停止Javascript中的setInterval循环而不刷新浏览器?

[英]How to stop a setInterval Loop in Javascript outside of code without refreshing the browser?

This may be a quite naive question but I really need some help. 这可能是一个非常幼稚的问题,但我确实需要一些帮助。

Prior to writing this post, I was programming on JSBin. 在写这篇文章之前,我是在JSBin上编程的。 Turns out without me realizing, I ran a setInterval loop prompting for userInput and it kept on looping, making me unable to click anywhere to change the code to fix the loop. 原来,我没有意识到,我运行了一个setInterval循环,提示输入userInput,并且该循环一直在循环,这使我无法单击任何位置来更改代码来修复该循环。 It kept on repeating and repeating. 它不断重复。 It got to the point where I had to refresh and lose all my hard-written-code (I was not logged in, so my code was not saved)! 到了必须刷新并丢失所有手写代码的地步(我没有登录,所以我的代码没有保存)! I want to avoid that next time. 我想下次避免。

So, my question is how do I stop any such kind of setInterval Loops, so that I am able to access my code and change it and re-run it. 因此,我的问题是如何停止任何此类setInterval循环,以便能够访问我的代码并对其进行更改并重新运行它。 Below is a code that demonstrates my issue, if you try running it on JSBin.com (obviously, it is not the code I wrote before). 如果您尝试在JSBin.com上运行它,则下面的代码演示了我的问题 (显然,这不是我之前编写的代码)。 As you can see, I can not click on my code to change it (or save it) in any way, which means I lose all my code! 如您所见,我无法单击我的代码以任何方式对其进行更改(或保存),这意味着我丢失了所有代码!

This may seem like a useless question, but I really want to know ways to fix it and perhaps fixing it from the developer tools will help me be familiar with the overwhelming set of tools it has :P. 这似乎是一个无用的问题,但是我真的很想知道修复它的方法,也许从开发人员工具中修复它可以使我熟悉它拥有的绝大多数工具:P。 So please help me if you know a solution. 因此,如果您知道解决方案,请帮助我。

Thank you for taking your time to help me! 感谢您抽出宝贵的时间来帮助我! I appreciate it. 我很感激。

setInterval(demo,1);
function demo()
{
     var name = prompt("Enter your name: ");
}

That's kind of a hack and should only be used in cases like the one exposed in OP but, 这是一种hack ,仅应在OP中公开的情况下使用,但是,
About all implementations use integers as timerid that just get incremented at every call. 几乎所有实现都使用整数作为timerid ,在每次调用时都会增加整数。

So what you can do, is to clear all timeouts that were created on the page. 因此,您可以做的是清除页面上创建的所有超时。
To do so you need to first get to which timerid we are, then call cleatTimeout or clearInterval (they do the same) in a loop until you reach the last call: 为此,您需要先到达我们所在的时区,然后循环调用cleatTimeout或clearInterval(它们执行相同的操作),直到到达最后一个调用:

 function stopAllTimers() { const timerid = setTimeout(_=>{}); // first grab the current id let i=0; while(i < timerid) { clearTimeout(i); // clear all i++; } }; btn.onclick = stopAllTimers; // some stoopid orphan intervals setInterval(()=>console.log('5000'), 5000); setInterval(()=>console.log('1000'), 1000); setInterval(()=>console.log('3000'), 3000); const recursive = () => { console.log('recursive timeout'); setTimeout(recursive, 5000); }; recursive(); 
 <button id="btn">stop all timeouts</button> 

Assuming the dev tools are closed, hit esc and f12 nearly simultaneously. 假设开发工具已关闭,则几乎同时按下escf12 This should open the dev tools. 这应该打开开发工具。 If it doesn't keep trying until it does. 如果它一直尝试直到它继续尝试。

Once they are open, hit esc and f8 . 打开它们后,按escf8 Again, retry til it halts javascript execution at some arbitrary point in the code. 再一次,重试直到它在代码中的任意点处停止javascript执行。

In the "sources" tab locate the generated script for what you wrote (offhand I don't know how it would look like from within JSBin) and literally delete the var name = prompt("Enter your name: "); 在“源”选项卡中,找到您编写的脚本(从JSBin中不知道它的外观),然后从字面上删除var name = prompt("Enter your name: "); line. 线。 Hitting f8 again will continue execution as if the "new" code is running. 再次点击f8将继续执行,就像“新”代码正在运行一样。 This should free you up to copy/paste your code from the site itself before you refresh the page 在刷新页面之前,这应该使您腾出时间从网站本身复制/粘贴代码。

Another option is to search the developer tools "Elements" panel for the iframe (this should be doable even if the main document is unresponsive due to prompt 's blocking) - then, just right click the iframe element and remove it, no need to type any Javascript. 另一种选择是在开发人员工具的“元素”面板中搜索iframe(即使由于prompt的阻塞而导致主文档无响应,此操作也应可行)-然后,只需右键单击iframe元素并将其删除,无需键入任何Javascript。 (or, if you want you can select the iframe with querySelector and remove it, eg document.querySelector('iframe').remove() ) (或者,如果您愿意,可以使用querySelector选择iframe并将其删除,例如document.querySelector('iframe').remove()

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

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