I would like to set an interval of 5 seconds between each alert. Found this thread:
setInterval(function() {
alert("Message to alert every 5 seconds");
}, 5000);
But where do I put the setInterval()
in order to alert every 5 seconds?
$(window).scroll(function() {
if (checkVisible($('#footer'))) {
alert("I DONT SEE A FOOTER");
} else {
alert("EUREKA - I SEE A FOOTER");
}
});
function checkVisible(elm, eval) {
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)));
}
Many thanks in advance.
You can put in load function
$(document).ready(function()
{
setInterval(function() {
alert("Message to alert every 5 seconds");
}, 5000);
});
Repeating every 5 seconds, started by scroll event:
var myTimer;
function showAlert(inString){
alert(inString);
myTimer = setTimeout(function() {
showAlert();
}, 5000);
}
$(window).scroll(function() {
clearTimeout(myTimer);
if (checkVisible($('#footer'))) {
showAlert("I DONT SEE A FOOTER");
} else {
showAlert("EUREKA - I SEE A FOOTER");
}
});
You can see that I'm using clearTimeout(myTimer)
to delete the previous timer, which avoids us starting the timer too many times. For this to work, we have to store the timer first - I've done it in the myTimer
variable.
Shows 5 seconds after scroll event but only once:
function showAlert(inString){
myTimer = setTimeout(function() {
alert(inString);
}, 5000);
}
$(window).scroll(function() {
clearTimeout(myTimer);
if (checkVisible($('#footer'))) {
showAlert("I DONT SEE A FOOTER");
} else {
showAlert("EUREKA - I SEE A FOOTER");
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With