当动态值达到设定限制时如何重置数组的值?

时间:2016-10-14 18:56:03

标签: javascript html angularjs

如果[]大于limit,我想将$scope.dynamic重置为$scope.event,因此,以下代码不会重置<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> 的值。我犯了错误?

main.html中

 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);
}

ctrl.js

$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; } 值...

printf %4d $(({1..10}*i))

2 个答案:

答案 0 :(得分:2)

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;
}

答案 1 :(得分:1)

现在您已经更新了代码,我现在明白$ scope.dynamic和totalLength都要包含$ scope.event数组的当前大小(以字节为单位)(不超过5000字节),而不是数组长度为我以前想过。看来你有很多变量存在于全局范围内,可能会在应用程序的其他地方被覆盖,所以这是可能的。你能发布一些console.log语句吗?除此之外,您是否尝试将.bind(this)添加到ditConsumer回调中?

socket.on('ditConsumer', function(data) {
    var obj = {
        file: $scope.filename,
        data: data
    }
    getByteLen(data);
    safelyAdd({
        id: $scope.event.length,
        value: data
    });

}.bind(this));