<div class="ads">Some text</div>
Want to:
How can I do this?
Site-examples with this feature:
http://premiumpixels.com
http://365psd.com
I've created example code that implement the requirement using cookie:
It depend on jQuery and Cookie plugin.
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<script type="text/javascript" src="https://github.com/carhartl/jquery-cookie/raw/master/jquery.cookie.js"></script>
<script type="text/javascript">
$(document).ready(function() {
if( $.cookie('showOnlyOne') ){
//it is still within the day
//hide the div
$('#shownOnlyOnceADay').hide();
} else {
//either cookie already expired, or user never visit the site
//create the cookie
$.cookie('showOnlyOne', 'showOnlyOne', { expires: 1 });
//and display the div
$('#shownOnlyOnceADay').show();
}
});
</script>
</head>
<body>
<div id="shownOnlyOnceADay">
This text should only be shown once a day!
</div>
</body>
</html>
Tested it by changing system date.
You can just use some code like this, assuming you always start off with your div
having its style display
property set to none
:
if(localStorage.last){
if( (localStorage.last - Date.now() ) / (1000*60*60*24) >= 1){ //Date.now() is in milliseconds, so convert it all to days, and if it's more than 1 day, show the div
document.getElementById('div').style.display = 'block'; //Show the div
localStorage.last = Date.now(); //Reset your timer
}
}
else {
localStorage.last = Date.now();
document.getElementById('div').style.display = 'block'; //Show the div because you haven't ever shown it before.
}
Reference
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