What is line 2 there for? Because it is there, the else block will never be reached.
If
If it's not supposed to be outside the function, you might want to create a closure. Then you don't have to initialize the variable every time it's called.
If
on
is supposed to be a global variable visible outside the function, you will need to declare it as a global.If it's not supposed to be outside the function, you might want to create a closure. Then you don't have to initialize the variable every time it's called.