简体   繁体   English

没有for的JavaScript无限循环

[英]Javascript endless loop without for

This function is running continuously without initiating a loop. 此功能在不启动循环的情况下连续运行。

The setTimeout is to allow the refreshTags function to run. setTimeout用于允许refreshTags函数运行。

I'm sure it's not the best-written script - I'm no guru - but any ideas on why this script is running in an endless loop? 我敢肯定这不是写得最好的脚本-我不是专家-但是关于为什么此脚本无休止地运行的任何想法吗?

function addTag() 
{
console.log('running'); 
refreshTags(); 
var t = document.getElementById('existingTags').textContent.match(/tag1/); 
var u = 'tag1'; 
if (t == u) {alert('This ticket has already been resolved by our team.')}; 
if (t != u) 
    {
    refreshTags();
    setTimeout(function() 
    {
        document.getElementById('tagToAdd').value = 'tag1';
        document.getElementById('tagSubmit').click(); 
        alert('Ticket resolved!'); 
    }, 2000)
}; 
}

Edit: code calling addTag below. 编辑:下面的代码调用addTag。

var resolveButton = document.createElement("a");
resolveButton.href = '#';
resolveButton.innerHTML = '<span>Resolve</span>';
resolveButton.setAttribute("onClick", "addTag()");
resolveButton.setAttribute("type", "button");
resolveButton.setAttribute("class", "button1");

var cha = document.getElementById('chatter_view');
cha.parentNode.insertBefore(resolveButton, cha);

Instead of trying to answer why this particular piece of code has an infinite loop, I'll attempt to answer the bigger question "How do you prevent and debug infinite loops?". 我将尝试回答一个更大的问题“如何防止和调试无限循环?”,而不是试图回答为什么这段特定代码具有无限循环。

One of the best defenses I've found is to structure your code so control is always flowing in one direction. 我发现的最好的防御方法之一就是构造代码,以使控制始终在一个方向上进行。 You have a number of different layers here: 您在这里有许多不同的层:

  • the onclick event handler is calling into addTag() onclick事件处理程序正在调用addTag()
  • addTag() is calling refreshTags() addTag()正在调用refreshTags()
  • addTag() is calling a setTimeout, which later triggers a click on the DOM. addTag()正在调用setTimeout,稍后将触发对DOM的单击。

A simple fix to keep your code flowing in one direction is to create dedicated event handlers, eg resolveButtonOnclick() { addTag() } . 使代码保持单一方向流动的一个简单解决方法是创建专用的事件处理程序,例如resolveButtonOnclick() { addTag() } resolveButtonOnclick is only ever called from the resolveButton's onclick handler. 仅从resolveButton的onclick处理程序中调用resolveButtonOnclick。 This makes it much easier to audit your code. 这使得审核代码变得更加容易。 You've already put a console.log('running') at the top of your addTag() function. 您已经将console.log('running')放在addTag()函数的顶部。 Now, if you put a console.log() in resolveButtonOnclick() you'll know right away if the onclick handler is included in your infinite loop. 现在,如果将console.log()放在resolveButtonOnclick()中,您将立即知道onclick处理程序是否包含在无限循环中。

We can't see your code, but if refreshTags() calls addTag() you would have a circular control flow -- these aren't always bad, but you need to be extra careful that they terminate at some point. 我们看不到您的代码,但是如果refreshTags()调用addTag(),您将拥有循环控制流-这些并不总是很糟糕,但是您需要格外小心,以免它们在某个时刻终止。

The biggest circular control flow you may have is that addTag() is calling back into the DOM with the .click() method. 您可能拥有的最大循环控制流程是addTag()使用.click()方法回调到DOM中。 It would be better, faster and cleaner to submit the form directly from the Javascript or use an XHR. 直接从Javascript或使用XHR提交表单会更好,更快和更干净。

To debug this loop, you are one the right track with the console.log()s. 要调试此循环,您可以使用console.log()正确地进行调试。 Add more of them (eg every place you call addTag()) and figure out where it is being called from. 添加更多它们(例如,每个您调用addTag()的位置)并找出从何处调用它。 The other thing you could try is using Chrome's DevTools: add a 'debugger;' 您可以尝试的另一件事是使用Chrome的DevTools:添加一个“调试器”; call to the top of addTag() and examine the stack trace. 调用addTag()的顶部并检查堆栈跟踪。

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

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