I'm trying to build a very simple interactive movie player on js just for fun. wireTo(x) triggers every scene by its own duration which predefined on same index. (This is my expectation)
I created a loop and put the setTimeout function inside of it. It iterates without problem on each duration property, but it couldn't handle with name properties(jumps to the last one).
var MoviePlayer = (function() {
function MoviePlayer(scenes) {
this.setSource(scenes);
}
MoviePlayer.prototype.setSource = function(_scenes) {
this.scenes = _scenes;
}
MoviePlayer.prototype.wireTo = function(number) {
var parts = this.scenes[number].parts;
for (var x in parts) {
var name = parts[x].name; // I think the problem starts here
setTimeout(function() {
alert(name);
}, parts[x].duration * x);
}
}
return MoviePlayer;
}());
// scenes configuration json
var scenes = [
{
ep: 1,
parts: [
{ name: "episode 1 p1", duration: 1000 },
{ name: "episode 1 p2", duration: 3000 }
],
next: 2
},
{
ep: 2,
parts: [
{ name: "episode 2 p1", duration: 1000 },
{ name: "episode 2 p2", duration: 1000 }
],
next: 3
}
];
// instantiation
let player = new MoviePlayer(scenes);
player.wireTo(0);
What's the problem with my logic. Any help would be very appreciated. Thanks.