loops - Read looping variable inside nested function Javascript -


for (var j=0; j<charts.length; j++){     var chart = charts[j].chartimage;     chart["export"].capture({}, function() {         this.topng({}, function(data) {             saved_charts[j] = data;             charts_remaining--;              if (charts_remaining == 0) {                 console.log(saved_charts[0]);                 console.log(saved_charts[1]);             }         })     }) } 

my problem on saved_charts[j] can't read j variable , says "mutable variable accessible closure". there other way can j variable? know can use array.push() reason code generate data value in irregular way doesn't return data value accordingly. think it's because data large function return smaller data first. sorry i'm still having hard time understanding javasript

this should works

for (let j=0; j<charts.length; j++){ 

if let keyword not supported, classic way:

for (var j=0; j<charts.length; j++){     var chart = charts[j].chartimage;     (function(j) {         chart["export"].capture({}, function() {             this.topng({}, function(data) {                 saved_charts[j] = data;             })         })     })(j); } 

Comments

Popular posts from this blog

vue.js - Create hooks for automated testing -

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

serial port - hub4com OVERRUN Error -