简体   繁体   English

Javascript运行时间过长

[英]Javascript taking too long to run

I have a script that is taking too long to run and that is causing me This error on ie : a script on this page is causing internet explorer to run slowly. 我的脚本运行时间过长,这导致我出现此错误,即:此页面上的脚本导致Internet Explorer运行缓慢。

I have read other threads concerning this error and have learned that there is a way to by pass it by putting a time out after a certain number of iterations. 我已经阅读了有关此错误的其他线程,并且了解到有一种方法可以通过在一定数量的迭代后超时来绕过它。

Can u help me apply a time out on the following function please ? 您能帮我在以下功能上申请超时吗?

Basically each time i find a hidden imput of type submit or radio i want to remove and i have a lot of them . 基本上,每次我找到要删除的类型为commit或radio的隐藏输入时,就会发现它们很多。 Please do not question why do i have a lots of hidden imputs. 请不要怀疑为什么我有很多隐藏的输入。 I did it bc i needed it just help me put a time out please so i wont have the JS error. 我做到了,因为我需要它只是帮助我超时,所以我不会出现JS错误。 Thank you 谢谢

$('input:hidden').each(function(){
    var name = $(this).attr('name');
    if($("[name='"+name+"']").length >1){
        if($(this).attr('type')!=='radio' && $(this).attr('type')!=='submit'){
            $(this).remove();
        }
    }
  });

One of the exemples i found : Bypassing IE's long-running script warning using setTimeout 我发现的示例之一: 使用setTimeout绕过IE的长时间运行的脚本警告

您可能想将input添加到jquery选择器中,以仅过滤出输入标签。

if($("input[name='"+name+"']").length >1){

Here's the same code optimised a bit without (yet) using setTimeout() : 这是使用setTimeout()尚未(尚未)优化的同一代码:

var $hidden = $('input:hidden'),
    el;
for (var i = 0; i < $hidden.length; i++) {
  el = $hidden[i];
  if(el.type!=='radio' && el.type!=='submit'
             && $("[name='" + el.name + "']").length >1) {

     $(el).remove();
  }
}

Notice that now there is a maximum of three function calls per iteration, whereas the original code had up to ten function calls per iteration. 注意,现在每个迭代最多可以有三个函数调用,而原始代码每个迭代最多可以有十个函数调用。 There's no need for, say, $(this).attr('type') (two function calls) when you can just say this.type (no function calls). 只需说出this.type (无函数调用),就不需要$(this).attr('type') (两个函数调用)了。

Also, the .remove() only happens if three conditions are true, the two type tests and check for other elements of the same name. 此外, .remove()仅在满足三个条件的情况下才会发生,这两个type进行测试并检查是否具有相同名称的其他元素。 Do the type tests first, because they're quick, and only bother doing the slow check for other elements if the type part passes. 首先进行type测试因为它们很快,并且只有在type部分通过的情况下,才需要对其他元素进行慢速检查。 (JS's && doesn't evaluate the right-hand operand if the left-hand one is falsy.) (如果左侧的操作数不正确,JS的&&不会评估右侧的操作数。)

Or with setTimeout() : 或使用setTimeout()

var $hidden = $('input:hidden'),
    i = 0,
    el;
function doNext() {
   if (i < $hidden.length) {
      el = $hidden[i];
      if(el.type!=='radio' && el.type!=='submit'
                && $("[name='" + el.name + "']").length >1) {

         $(el).remove();
      }
      i++;
      setTimeout(doNext, 0);
   }
}
doNext();

You could improve either version by changing $("[name='" + el.name + "']") to specify a specific element type, eg, if you are only doing inputs use $("input[name='" + el.name + "']") . 您可以通过更改$("[name='" + el.name + "']")来指定特定的元素类型来改进任一版本,例如,如果您仅在使用输入,则使用$("input[name='" + el.name + "']") Also you could limit by some container, eg, if those inputs are all in a form or something. 同样,您可以限制某个容器,例如,如果这些输入全部为某种形式或某种形式。

It looks like the example you cited is exactly what you need. 看来您引用的示例正是您所需要的。 I think if you take your code and replace the while loop in the example (keep the if statement for checking the batch size), you're basically done. 我认为,如果您拿起代码并替换示例中的while循环(保留if语句以检查批处理大小),则基本上已经完成。 You just need the jQuery version of breaking out of a loop. 您只需要打破循环的jQuery版本即可。

Try this: 尝试这个:

$("[type=hidden]").remove(); // at the place of each loop

It will take a short time to delete all hidden fields. 删除所有隐藏字段将花费很短的时间。

I hope it will help. 希望对您有所帮助。

JSFiddle example JSFiddle示例

To risk stating the obvious; 冒着陈述明显的风险; traversing through the DOM looking for matches to these CSS selectors is what's making your code slow. 遍历DOM寻找与这些CSS选择器的匹配项是使您的代码变慢的原因。 You can cut down the amount of work it's doing with a few simple tricks: 您可以通过一些简单的技巧来减少工作量:

Are these fields inside a specific element? 这些字段是否在特定元素内? If so you can narrow the search by including that element in the selector. 如果是这样,您可以通过在选择器中包含该元素来缩小搜索范围。

eg: 例如:

$('#container input:hidden').each(function(){
    ...

You can also narrow the number of fields that are checked for the name attribute 您还可以缩小检查name属性的字段数

eg: 例如:

if($("#container input[name='"+name+"']").length >1){

I'm also unclear why you're searching again with $("[name='"+name+"']").length >1 once you've found the hidden element. 我也不清楚为什么找到隐藏元素后,为什么要再次使用$("[name='"+name+"']").length >1 You didn't explain that requirement. 您没有解释该要求。 If you don't need that then you'll speed this up hugely by taking it out. 如果您不需要它,则可以通过将其取出来大大加快该过程。

$('#container input:hidden').each(function(){
    var name = $(this).attr('name');
    if($(this).attr('type')!=='radio' && $(this).attr('type')!=='submit'){
        $(this).remove();
    }
});

If you do need it, and I'd be curious to know why, but the best approach might be to restructure the code so that it only checks the number of inputs for a given name once, and removes them all in one go. 如果您确实需要它,我很想知道为什么,但是最好的方法可能是重组代码,以便它只检查一次给定名称的输入数量,并一次性删除所有输入。

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

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