Reputation: 1488
i have an array,
[0][video object]
[0][state]
[0][parameters]
[1][video object]
[1][state]
[1][parameters]
.
.
.
i want to know where all videos in array can be played:
for(i=0;i<videos.length;i++){
$(videos[i]['video']).on('canplaythrough', function(){
//do stuff - check all
update parent > ['state'] to true;
}
}
The problem is after i loose the index i. How can i pass it inside on() function? i want to get the parent of the array item.
Upvotes: 0
Views: 84
Reputation: 2456
to use the var i
inside your on
function you can do the following
for(i=0;i<videos.length;i++){
$(videos[i]['video']).on('canplaythrough', function(i){
console.log(i); // i is accessible here now
//do stuff - check all
videos[i]['state'] = true;
}
}
Upvotes: 1
Reputation: 388356
You are loosing the value of i
because it is a closure variable, in the given case since you are iterating through an array I think it is better to use $.each() here
$.each(videos, function(idx, video){
$(video.video).on('canplaythrough', function(){
//do stuff - check all
video.state = true
}
})
Upvotes: 2