Skip to content Skip to sidebar Skip to footer

Alternative To The 'let' Keyword In A Loop?

I was having an issue where I was setting up a bunch of timeout calls and I wanted the timeout function to reference the element in the current loop. This is what the result looked

Solution 1:

Use a IIFE:

var createTimedMessages = function(arr, collection) {
    var timeLimit = 2000;
    for (var i = 0; i < arr.length; i++) {
        (function(el) {
            collection.push(el);
            $timeout(functionremoveElement() {
                collection.removeMatch(el);
            }, timeLimit);
        })(arr[i]);
    }
}

Solution 2:

Self-executing functions should solve the problem:

(function(el, timeLimit) {
    $timeout(/* your code */);
})(arr[i], timeLimit);

Post a Comment for "Alternative To The 'let' Keyword In A Loop?"