[英]when adding new element to the end of array it changes using splice?
When $scope.event
reach BufferLimit i am removing first element from an array that will reduce totalReceived
value than i am pushing new element outside if statement, but when i push new element i dont see that as last item in array ? 当
$scope.event
到达BufferLimit时,我要从数组中删除第一个元素,这会比我在if语句外推送新元素时减少totalReceived
值,但是当我推送新元素时,我没有看到它作为数组中的最后一项? I also use slice
that froze my browser at bufferlimit with shift
it is same as slice
. 我还使用了
slice
,它将我的浏览器冻结在bufferlimit处,与shift
相同,它与slice
相同。 I am looking for better solution because of high data volume ? 由于数据量大,我正在寻找更好的解决方案吗?
Ctrl.js 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..
}
In your code line# 4, dont assign spliced array to same array.Just splice it
as below 如下
$scope.event.splice(1); $ scope.event.splice(1); //delete first element in $scope.event
//删除$ scope.event中的第一个元素
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.