Last active
January 17, 2017 19:13
-
-
Save Chryus/04089271a2caf01aeb7a9f5035e1d7f4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// In the initial code, I wasn't aware that let would create a new binding for | |
// each variable inside my loop. I assumed a closure around the setTimeout function | |
// was necessary to create new copy of the variables at each step. | |
animate() { | |
let previousDelay = 0; | |
let styles = null; | |
for (let sequence of this.dataSet) { | |
let delay = sequence[0] + previousDelay; | |
let objects = sequence.filter(this.isObject); | |
styles = this.formatStyles(objects); | |
this.handleTimer(delay, styles); | |
previousDelay = delay; | |
} | |
styles; | |
} | |
handleTimer(delay, styles) { | |
let timerID = setTimeout(() => { | |
console.log(delay); | |
this.element.style = styles | |
}, delay); | |
return timerID | |
} | |
// In the refactored solution, I realized the closure was superflous | |
// double yay, let = concise code | |
animate() { | |
let delaySoFar = 0; | |
let styles = null; | |
for (let sequence of this.dataSet) { | |
let delay = sequence[0] + delaySoFar; | |
let objects = sequence.filter(this.isObject); | |
let styles = this.formatStyles(objects); | |
setTimeout(() => { | |
this.element.style = styles; | |
}, delay); | |
delaySoFar = delay; | |
} | |
styles; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment