I've been working on setting up a basic loop of animation functions. Each function is stored in an array, and I have successfully logged out each object as a string upon click event. However, I'm encountering difficulties when trying to call the corresponding functions during that event.
One of my attempts led to an error message stating "nt[rt] is not a function".
arrAnimations[activeScene]()
In my effort to resolve this issue, I explored various solutions from similar scenarios on stack overflow. One approach involved creating a helper function like the one below:
myFunction = function(){};
var arrAnimations = {italy: myFunction};
arrAnimations['activeScene'](); //executes the function
I also experimented with another method, as shown here:
var tmp = arrAnimations[activeScene]
window[tmp]
Below is the code snippet:
var arrAnimations = ['italy', 'czech', 'russia']
var activeScene = 0;
document.getElementById('animate').addEventListener("click", function incNumber() {
if (activeScene < arrAnimations.length - 1) {
activeScene++;
} else if (activeScene == arrAnimations.length - 1) {
activeScene = 0;
}
})
function italy() { console.log('italy') }
function czech() { console.log('czech') }
function russia() { console.log('russia') }
<div id="animate">Animate</div>