[Vuejs]-Vue js method with argument and setTimeout

0๐Ÿ‘

โœ…

You cant reference a parameter and expect it to be changed outside, but you can pass a reference to an object that can change something outside.

var $this = this;
this.counter({
    get() { return $this.userMinerals },
    set(val) { $this.userMinerals = val }
});

and then use in the counter like this

    counter(typeOfCredits) {
        setInterval(() => {
            typeOfCredits.set(typeOfCredits.get() + this.miners);

            if(this.checkLimit(this.userMinerals, this.mineralsLimit)) {
                typeOfCredits.set(this.mineralsLimit);
            }
        }, 100);
    },

jsfiddle

๐Ÿ‘คEvaldo Bratti

0๐Ÿ‘

typeOfCredits is a parameter to the function. Parameters are passed by value. Modifying it is like modifying a local variable.

๐Ÿ‘คRoy J

Leave a comment