I have created a small piece of code that creates a DIV at a random 'fixed' location every 80ms. This div shows up for about 2seconds and then removes itself (including the code generated). However when leaving the part of the page where this is happening (using scrolling); I want the generator to stop.
And to avoid executing the code everytime the user uses his scroll (duplicate generators, because of scrolling) I thought it would be most useful to use a true/false statement.
if (focuson = true){window.setInterval(function(){$(generator);}, 80);}
$(window).scroll(function(){
if ($(window).scrollTop() > resolutieh){
var focuson = false;
window.clearInterval(generator); generator = 0;
}
else{
var focuson = true;
}
});
The code above actually works but results in a visual glitch; when the last generated DIV gets removed: every part of the screen at that moment, that is not 'filled' with div-content will be filled with big white chunks (basically the 'background-image' on repeat x/y). Also scrolling back to the location where the generator is active doesnt activate it again.
The following piece of code seems more logical to me (although it doesnt work):
if (focuson = true){window.setInterval(function(){$(generator);}, 80);}
if (focuson = false){window.clearInterval(function(){$(generator);}, 0);}
$(window).scroll(function(){
if ($(window).scrollTop() > resolutieh){
var focuson = false;;
}
else{
var focuson = true;
}
});
I basically want to avoid the visual glitch by either stopping the generator 'properly' or setting its timer so low that it takes less time to calculate. A cool bonus would be that if you would come back to the part of the page where the generator is spawning, it would turn on again.
Any suggestions?
Kind Regards.