Firefox的循环引发js错误“分配大小溢出” [英] Firefox throwing js error in for loop "allocation size overflow"

查看:163
本文介绍了Firefox的循环引发js错误“分配大小溢出”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的代码



相同的代码在本地服务器上工作,但不在线。

  htmlC =; 
htmlC + ='< select name =pagenumid =pagenumstyle =width:135pxonChange =setPageSearchValue(this.value)>'; (i = 1; i< = tot_pages; i ++)
($ i $ to b(b)if(i.toString()== document.frmlist.start.value)
{
htmlC + =< option value ='+ i +''selected'>+ i +< / option>;
}
else
{
htmlC + =< option value ='+ i +'>+ i +< / option>;
}
}
htmlC + ='< / select>';

我试过找到无限循环但没有成功。在本地服务器上使用非常相同的代码。

解决方案

以这种方式使用字符串连接通常是一个坏主意,特别是如果你不不知道你将要做的迭代次数。每次你连接一个字符串,你将重新分配所需的内存来适应新的字符串,并需要垃圾收集旧的(这可能不会在循环期间出于性能原因)

  var htmlBuffer = []; 
htmlBuffer.push('< select name =pagenumid =pagenumstyle =width:135pxonChange =setPageSearchValue(this.value)>'); (i = 1; i< = tot_pages; i ++)
($ i $ to b(b)if(i.toString()== document.frmlist.start.value)
{
htmlBuffer.push(< option value ='+ i +''selected'>+ i +< / option>);


{
htmlBuffer.push(< option value ='+ i +'>+ i +< / option>);
}
}
htmlBuffer.push('< / select>');

htmlC = htmlBuffer.join('\\\
');

上面将定义一个数组,您将每个行推送到该数组上。它将动态分配扩展数据所需的内存,最后为数据总量分配1个字符串。这是更有效率。我不知道这是否是你的情况下的实际问题(因为我们不知道什么是tot_pages),但是避免字符串在循环中连接是一个不错的主意。

Below is my code

Same code is working in local server but not in live.

    htmlC = "";
    htmlC += '<select name="pagenum" id="pagenum" style="width:135px" onChange="setPageSearchValue(this.value)">';
    for(i=1 ; i<=tot_pages ; i++)
    {
            if(i.toString() == document.frmlist.start.value)
            {
                htmlC += "<option value='"+i+"' 'selected' >"+i+"</option>";
            }
            else
            {
                htmlC += "<option value='"+i+"'>"+i+"</option>";
            }
    }   
    htmlC += '</select>';

I have tried finding infinite loop but no success. Very same code is working in local server.

解决方案

Using string concatenation in this manner is usually a bad idea, especially if you don't know the number of iterations you will be doing. Every time you concatenate a string, you will reallocate the memory needed to fit the new string and need to garbage collect the old one (which might not even be done during the loop for performance reasons)

var htmlBuffer = [];
htmlBuffer.push('<select name="pagenum" id="pagenum" style="width:135px" onChange="setPageSearchValue(this.value)">');
for(i=1 ; i<=tot_pages ; i++)
{
        if(i.toString() == document.frmlist.start.value)
        {
            htmlBuffer.push("<option value='"+i+"' 'selected' >"+i+"</option>");
        }
        else
        {
            htmlBuffer.push("<option value='"+i+"'>"+i+"</option>");
        }
}   
htmlBuffer.push('</select>');

htmlC = htmlBuffer.join('\n');

The above will define an array, to which you push each "row" onto. It will dynamically allocate memory needed for the expanding data, and finally, you allocate 1 string for the total amount of data . This is much more efficient. I don't know if this is the actual problem in your case (since we don't know what tot_pages are), but it's never a bad idea to avoid string concatenations in loops anyway.

这篇关于Firefox的循环引发js错误“分配大小溢出”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆