如果 limit
大于 $scope.dynamic
,我想将 $scope.event
重置为 []
> ,所以下面的代码没有重置 $scope.event
的值知道我哪里出错了吗?
main.html
<ul style="list-style: none;">
<li ng-repeat="message in event track by $index | limitTo:500" ng-class="{lastItem: $last}"><span><strong>Log:</strong></span><span>{{message}}</span></li>
</ul>
控制键
var limit = 5000;
$scope.event = [];
var totalLimit;
$scope.dynamic = this numeric value is changing based on data;
socket.on('ditConsumer', function(data) {
var obj = {
file: $scope.filename,
data: data
}
getByteLen(data);
safelyAdd({
id: $scope.event.length,
value: data
});
});
function safelyAdd(element) {
if (totalLimit > limit) {
$scope.event = []; //reset array if max size reached..
}
$scope.event.push(element); //then push new item..
console.log('array', $scope.event);
console.log('totalLimit', totalLimit);
}
在这些函数中获取 $scope.dynamic
值...
$scope.random = function(value) {
$scope.dynamic = value;
totalLimit = value;
$scope.downloadPercentage = parseFloat((value/$scope.maxBytes) * 100).toFixed(0);
console.log('current value-dynamic',$scope.dynamic);
};
function getByteLen(normal_val) {
// Force string type
normal_val = String(normal_val);
var byteLen = 0;
for (var i = 0; i < normal_val.length; i++) {
var c = normal_val.charCodeAt(i);
byteLen += c < (1 << 7) ? 1 :
c < (1 << 11) ? 2 :
c < (1 << 16) ? 3 :
c < (1 << 21) ? 4 :
c < (1 << 26) ? 5 :
c < (1 << 31) ? 6 : Number.NaN;
}
currentBytesSum = currentFileBytes.reduce(function(a, b) { return a + b; }, 0);
$scope.random(currentBytesSum);
formatBytes(currentBytesSum);
return byteLen;
}
function formatBytes(bytes,decimals) {
if(bytes == 0) return '0 Byte';
var k = 1000;
var dm = decimals + 1 || 3;
var sizes = ['Bytes', 'KB', 'MB'];
var i = Math.floor(Math.log(bytes) / Math.log(k));
var data = parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
console.log('sum of all the bytes', data);
$scope.currentBytes = data;
}
最佳答案
var limit = 5000;
$scope.dynamic = 0;
var totalReceived = 0;
socket.on('ditConsumer', function (data) {
var byteLength = getByteLen(data);
$scope.dynamic += byteLength;
totalReceived += byteLength;
$scope.$apply(function() {
safelyAdd({
id: $scope.event.length,
value: data
});
});
});
function safelyAdd(element) {
if (received > limit) {
$scope.event = []; //reset array if max size reached..
$scope.dynamic = 0;
}
$scope.event.push(element); //then push new item..
console.log('array', $scope.event);
console.log('totalLimit', totalLimit);
}
$scope.random = function (value) {
$scope.downloadPercentage = parseFloat((value / $scope.maxBytes) * 100).toFixed(0);
console.log('current value-dynamic', $scope.dynamic);
};
function getByteLen(normal_val) {
// Force string type
normal_val = String(normal_val);
var byteLen = 0;
for (var i = 0; i < normal_val.length; i++) {
var c = normal_val.charCodeAt(i);
byteLen += c < (1 << 7) ? 1 :
c < (1 << 11) ? 2 :
c < (1 << 16) ? 3 :
c < (1 << 21) ? 4 :
c < (1 << 26) ? 5 :
c < (1 << 31) ? 6 : Number.NaN;
}
currentBytesSum = currentFileBytes.reduce(function (a, b) {
return a + b;
}, 0);
formatBytes(currentBytesSum);
return byteLen;
}
function formatBytes(bytes, decimals) {
if (bytes == 0) return '0 Byte';
var k = 1000;
var dm = decimals + 1 || 3;
var sizes = ['Bytes', 'KB', 'MB'];
var i = Math.floor(Math.log(bytes) / Math.log(k));
var data = parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
console.log('sum of all the bytes', data);
$scope.currentBytes = data;
}
关于javascript - 当动态值达到设置限制时如何重置数组的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40050236/