This is a very simple question/problem, and I can easily work around it, but since I’m learning javascript I was very eager to know WHY exactly this particular problem is happening.
$("#go").click(function() {
$("p").append(array[x] + " ")
functionlist[array[x]]()
x++
})
This does not work as I expect it to. I want it to write the current content of array, perform a simple animation that is associated with a certain function name, and then increment x. Everything works, except it doesn’t increment x.
If I do this:
$("#go").click(function() {
$("p").append(array[x] + " ")
//functionlist[array[x]]()
x++
})
x is incremented successfully.
So my question is, why does this happen?
Here is a link to a jsfiddle that I am using: http://jsfiddle.net/mxy6N/3/
Well, if you check your script console (F12 is most browsers), you’ll see that
functionlist[array[x]]()throws an error something like:This is because
array[x]is equal to “smooch”, andfunctionlist["smooch"]is undefined, so it errors out before it makes it to yourx++.Other things going on in this code:
flipandflopboth have the samerotateYvalue, so once it “flips” it won’t “flop”Here is something that might do what you’re looking to do: http://jsfiddle.net/g5mJd/3/
And the updated code:
EDIT: Updated with
$.isFunction()check.