Basically you want to alert every 5 seconds but also check every 5 seconds if you have a footer or not ?
Then you add this :
setInterval(function() {
if (checkVisible($('#footer'))) { //check footer
alert("I DONT SEE A FOOTER");
} else {
alert("EUREKA - I SEE A FOOTER");
}
}, 5000);
This will show an alert every 5 seconds, the text on that alert will depend if you have a footer or not :)
But you don't want to call this every time you scroll (it will run around 12 time per scroll, which is not what you want). So you can either start it on load, or you can do as I've done and run the timer once when you start scrolling.
So ive created a function that sets up an interval :
function showAlert() {
setInterval(function() {
if (checkVisible($('#footer'))) {
//alert("I DONT SEE A FOOTER");
outputString = "EUREKA - I SEE A FOOTER";
} else {
//alert("EUREKA - I SEE A FOOTER");
outputString = "I DONT SEE A FOOTER";
}
console.log(outputString)
}, 5000);
}
Here I've used console.log() rather than alert as alerts are, to be honest, super annoying. This timer also checks if there is a footer and logs accordingly.
Now I've added a bool that is true but when scrolled gets set to false so I only run the above function once when scrolled:
var runOnceScrolled = true;
$(window).scroll(function() {
console.log('scrolled, timer will now start')
if (runOnceScrolled) {
showAlert();
runOnceScrolled = false;
}
});
I have also wrapped the contents of your checkVisible in a try catch as it was throwing errors if #footer
wasn't there.
I've added a button also, to insert a div with ID of footer
just so you can see the console.log() changes after you add the footer.
$('#addFooter').click(function(d) {
console.log('add footer')
$("body").append("<div id='footer'>This is the footer</div>")
})
var outputString = "";
function showAlert() {
setInterval(function() {
if (checkVisible($('#footer'))) {
//alert("I DONT SEE A FOOTER");
outputString = "EUREKA - I SEE A FOOTER";
} else {
//alert("EUREKA - I SEE A FOOTER");
outputString = "I DONT SEE A FOOTER";
}
console.log(outputString)
}, 500);
}
console.log('outputString : ' + outputString)
var runOnceScrolled = true;
$(window).scroll(function() {
if (runOnceScrolled) {
console.log('scrolled, timer will now start')
showAlert();
runOnceScrolled = false;
}
});
function checkVisible(elm, eval) {
try {
eval = eval || "visible";
var vpH = $(window).height(), // Viewport Height
st = $(window).scrollTop(), // Scroll Top
y = $(elm).offset().top,
elementHeight = $(elm).height();
if (eval == "visible")
return ((y < (vpH + st)) && (y > (st - elementHeight)));
if (eval == "above")
return ((y < (vpH + st)));
} catch (err) {
//console.log(err)
}
}
.outside {
color: red;
border: 1px solid black;
position: absolute;
height: 150%;
width: 100px;
overflow: scroll;
}
.inside {
width: 100%;
height: 400px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id='addFooter'>
Add footer
</button>
<div id="outside">
<div class='inside'></div>
<div class='inside'></div>
<div class='inside'></div>
</div>
Working fiddle : https://jsfiddle.net/reko91/xQacd/459/