0

调用函数时removeCell(),Firefox 冻结。

我使用此功能隐藏一些网格单元格,具体取决于窗口宽度。
在网格准备好并且每次调整窗口大小后,函数都会被调用。
在 Chrome 和 Opera 中工作得很好,但在 Firefox 中它在第一次调用后就卡住了。

function removeCell(){
    headerResize();
    if (($('.section .header .cell:last-child').position().left + $('.section .header .cell:last-child').width()) > 
        $('.section .header').position().left + $('.section .header').width())
    {
        var priority = $body_grid_header[0].priority;
        var index    = 0;

        for(var i in $body_grid_header){
            if(bool($body_grid_header[i].visible) && $body_grid_header[i].priority > priority){
                priority = $body_grid_header[i].priority;
                index    = i;
            }
        }
        $body_grid_header[index].visible = 0;
        $('.grid .header .cell:nth-child('+ (parseInt(index)+2) +')').addClass('hidden');

        $hiddenArray.unshift(index);
        headerResize();
        removeCell();
    }
    else
    {
        //console.log($body_grid_header);
        var firstCell   = $('.grid .header .cell:first-child').width();
        var lastCell    = $('.grid .header .cell:last-child').width();
        var headerWidth = $('.grid .header').width();
        var cellCount   = $('.grid .header .cell').not(':first').not(':last').not('.hidden').length;

        if ((cellCount*100 + 100+firstCell+lastCell < headerWidth) && (cellCount < $body_grid_header.length))
        {
            var index = $hiddenArray[0];
            $hiddenArray.splice(0,1);

            $body_grid_header[index].visible = 1;
            $('.grid .header .cell:nth-child('+ (parseInt(index)+2) +')').removeClass('hidden');
            headerResize();
            removeCell(); //I GUESS IT STUCKS HERE!
        }
    }
}


function headerResize(){
  var firstColl = $('.grid .header .coll:first-child').width();
  var lastColl  = $('.grid .header .coll:last-child').width();
  var headerWidth   = $('.grid .header').width() - firstColl - lastColl;
  var collCount = $('.grid .header .coll').not(':first').not(':last').not('.hidden').length;
  var collWidth     = headerWidth / collCount - 1; //-1 = border-left

  if(collWidth < 100) collWidth = 100;

  $('.section .header .coll').not(':first').not(':last').width(collWidth);
  $('.section .content .coll:not(:first-child)').width(collWidth);

}

4

2 回答 2

0

很难理解,很难在本地测试:-)

试一试,改变

if ((cellCount*100 + 100+firstCell+lastCell < headerWidth) && (cellCount < $body_grid_header.length))

if ((cellCount*100 + 100+firstCell+lastCell < headerWidth) && $hiddenArray.length)

希望它有效...

于 2012-08-21T12:13:02.360 回答
0

该代码可能会导致任何浏览器出现死锁,因为它归结为:

removeCell() {
     removeCell();
}

唯一可以阻止的是if()else分支中:

if ((cellCount*100 + 100+firstCell+lastCell < headerWidth) && (cellCount < $body_grid_header.length))

如果这个条件是错误的,你有一个无限循环。

而不是递归调用,您应该真正使用循环并在删除所有单元格时退出它。

于 2012-08-21T11:58:44.563 回答