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

标签 javascript html angularjs

如果 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/

相关文章:

javascript - 在使用 iOS Phonegap 包装的 jQuery Mobile 中找不到具有相对 src 的图像

javascript - 比较angularjs指令中的两个字段

javascript - <ng-show> 在 Angular Materials 上隐藏时显示为文本

javascript - 将单个参数传递给具有默认值的函数

javascript - 我们可以在 css 中使用百分比高度吗?

javascript - 异步/等待与 Promise

html - 为什么我的 div 也不能按照我想要的方式对齐,因为当我尝试定位它们时它们一直重叠

css - 如何通过在单个 http 请求中获取多个图像来提高性能?

javascript - 如何在运行时获取 Javascript 函数调用/跟踪

javascript - RegEx 表达式或 jQuery 选择器不匹配 href 中的 "external"链接