Problems with js scope

dom:

<view :animation="barr.animationIndex" v-for="(barr,index) in dataBarrage" :key="index">{{barr.value}}</view>

js:

    var _this = this;

    for (let i = 0; i < _this.dataBarrage.length; iPP) {
        var num = Math.random(); //
        var item = _this.dataBarrage[i];
        _this.$set(item, "x", _this.windowWidth + 200 * num);
        _this.$set(item, "y", num * 100 + 30);
        _this.$set(item, "animationIndex", "animationAll" + i); //
        //
        _this.animation.translate(item.x, item.y).step({ duration: 10 });
        item.animationIndex = _this.animation.export();     //
                
        setTimeout(function() {                    
           _this.animation.translate(-_this.windowWidth - 300, item.y).step();
           item.animationIndex = _this.animation.export();
        }.bind(_this),1000);
    }

I assign a value to item.animationIndex in the setTimeout function. Why only the last value takes effect? the first assignment of item.animationIndex before is all correct after the loop. In the setTimeout function, only the last assignment to item.animationIndex takes effect. What"s going on? How should I write it?

Nov.06,2021

(function(item){
  setTimeout(function() {                    
    _this.animation.translate(-_this.windowWidth - 300, item.y).step();
    item.animationIndex = _this.animation.export();
  }.bind(_this),1000);
})(item);

let item = _this.dataBarrage[i];

your item is not a local variable. It is equivalent to declaring that each assignment is an override outside the for loop that causes the last value of the override to be read when the function executes setTimeout .


javascript Advanced programming says that this objects are bound at run time based on the function's execution environment: in global functions, this equals window, and when the function is called as an object, this equals that object. However, anonymous functions are global, so this objects often point to window

however, the view that anonymous function this is global is still controversial. For more information on this scope, please see
[Javascript] to understand the this scope problem and the influence of new operator on this scope

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3ccf9-2c33d.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3ccf9-2c33d.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?