2017-03-28 108 views
0

$scope.event达到BufferLimit我从一个阵列,这将减少totalReceived值比我外面的if语句推新元素删除第一个元素,但是当我把新元素我没有看到在阵列,作为最后一个项目?我也用slice是冻结了我的浏览器在bufferlimit与shift是一样slice。由于数据量高,我正在寻找更好的解决方案?当添加新的元素到数组的末尾它使用拼接改变?

Ctrl.js如下

//BufferLimit is 8MB totalReceived value is coming from other method 

$scope.event = []; 
    function safelyAdd(element) { 
     if (totalReceived > Bufferlimit && $scope.event.length) { 

    $scope.event = $scope.event.splice(1); //delete first element in $scope.event 

      totalReceived -= $scope.event[0].messageSize; //total message size minus deleted message size 
      console.log('totalReceivedBytes', totalReceived); 
      // $scope.event =[];//reset array if max size reached.. 
      console.log('$scope.event', $scope.event) 
     } 
     console.log('$scope.event.length', $scope.event.length); 

      $scope.event.push(element); //then push new item.. 

    } 

回答

0
In your code line# 4, dont assign spliced array to same array.Just splice it 


$ scope.event.splice(1); //删除$ scope.event中的第一个元素

+0

所以只需创建新的数组对象'var temp = $ scope.event.splice(1);' – hussain

+0

哦你说的只是拼接它 – hussain

+0

但我看到'$ scope.event .length'不停地用新的消息越来越多,甚至我们'splice'第一要素,即实际的目的,以减少对浏览器 – hussain

相关问题