Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show div every 5 minutes javascript

I have a javascript code that is only showing the DIV one time after 5 minutes, but I want that script to execute/show the DIV id "off" again every 5 minutes...

How can I do that?

<div id="off">
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript" src="test.js"></script>
</div>

<script>
var div = document.getElementById('off');
div.style.display = 'none';
setTimeout(function() {
    div.style.display = 'block';
}, 5 * 60000);
</script>
like image 922
Lucas Abade Avatar asked Mar 24 '26 02:03

Lucas Abade


1 Answers

Using setInterval()

setInterval(function() {
    div.style.display = 'block';
}, 5 * 60000);

If at some point you want to stop your interval ticking, you should use it assigned to a function expression like:

var myIntv = setInterval(function() {
    div.style.display = 'block';
}, 5 * 60000);


// (let's say we're inside a button click event)
// clearInterval( myIntv ); // Stop it

jsBin demo


Using setTimeout()

function doSomething (){
    div.style.display = 'block';
    setTimeout(doSomething, 5 * 60000); // Recall
}

doSomething(); // Start

jsbin demo

like image 68
Roko C. Buljan Avatar answered Mar 26 '26 14:03

Roko C. Buljan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!