Is there a way to create a variable in jQuery / JavaScript if it not exists?
I've tried:
if (moving.length<0) {
moving=false;
}
if (moving=='undefined') {
moving=false;
}
But logically it all failes because the initial check on the variable is already undefined.
Hope you guys can help :)
EDIT
First answer worked properly but my problems with this continue.
$.fn.animation = function() {
if (typeof moving == 'undefined') {var moving = false;};
if(moving===false){
var moving=true;
console.log('test');
};
};
$(window).scroll(function(){
if($(window).scrollTop() < scrollpos){
elm.animation();
}
scrollpos=$(window).scrollTop();
});
In this setup 'test' gets constantly logged even if I log moving which is set to true the if still gets ignored.
So what I want to archive is that every time I scroll the element get checked if its already animated. If so, don't do the animation.
Basically a queue.