admin管理员组文章数量:1415664
I'm trying to make a wele message for the user.
My planning is:
- if the user visits the first time the page then show a
div
message after 30 sec.; - if the user already visited the page there is no need to show
div
message.
I have writed this code:
My cookies code
<script type="text/javascript">
function createCookie(name, value, days) {
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
var expires = "; expires=" + date.toGMTString();
} else var expires = "";
document.cookie = name + "=" + value + expires + "; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name, "", -1);
}
</script>
I'm trying to make a wele message for the user.
My planning is:
- if the user visits the first time the page then show a
div
message after 30 sec.; - if the user already visited the page there is no need to show
div
message.
I have writed this code:
My cookies code
<script type="text/javascript">
function createCookie(name, value, days) {
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
var expires = "; expires=" + date.toGMTString();
} else var expires = "";
document.cookie = name + "=" + value + expires + "; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name, "", -1);
}
</script>
My display code
<script type="text/javascript">
function showbox() {
document.getElementById("apDiv1").style.visibility = "visible";
}
setTimeout("showbox()", 30000); // after 5 secs
</script>
<script type="text/javascript">
if (!readCookie('visitedPreviously')) {
showbox();
//document.write(' Your Message Goes Here And You See It Only Once ');
createCookie('visitedPreviously', 'visitedPreviously', 3); // 365 days persistence
}
//eraseCookie('visitedPreviously'); // FOR TEST PURPOSES
</script>
My problem is when a visitor visits the page first time, the div shows immediately (30 sec.) - Doesn't work
But when a returning visitor visits again the page the div
shows after 30 sec.
I want for a first time user visit to show a div message after 30 sec.
Please help me.
Share Improve this question edited Jun 2, 2015 at 10:36 Tiberiu C. 3,5231 gold badge33 silver badges41 bronze badges asked Jun 2, 2015 at 9:06 Shanmuganathan ShanshayanShanmuganathan Shanshayan 697 bronze badges 3- The reason that it doesn't work is that you pass in the call to the function. that call is immediately evaluated. Use setTimeout(showBox, 30000); – Hauke S Commented Jun 2, 2015 at 9:10
-
change return null to return false in
readCookie
. maybe this will callshowbox
function first .. i think not problem withsetTimeout
function – Nishit Maheta Commented Jun 2, 2015 at 9:16 -
I think the divs shows immediately because
if (!readCookie('visitedPreviously')) {
is getting executed. And there is NO problem withsetTimeout
– Shaunak D Commented Jun 2, 2015 at 9:16
7 Answers
Reset to default 3According to your explanation you want to show a dialog after 30 seconds only to new user. So you need to change your code into below:
<script type="text/javascript">
function showbox() {
document.getElementById("apDiv1").style.visibility = "visible";
}
if (!readCookie('visitedPreviously')) { //if he/she is a new user
setTimeout("showbox()", 30000); // after 30 secs
//document.write(' Your Message Goes Here And You See It Only Once ');
createCookie('visitedPreviously', 'visitedPreviously', 3); // 365 days persistence
}
//eraseCookie('visitedPreviously'); // FOR TEST PURPOSES
</script>
You need to remove quotes around showbox.
This the syntax of the setTimeout fnction,
setTimeout(function,milliseconds,param1,param2,...)
setTimeout expects the first parameter to be a function call.
function showbox() {
document.getElementById("apDiv1").style.visibility = "visible";
alert();
}
setTimeout(showbox, 30000); // after 5 secs
if (!readCookie('visitedPreviously')) {
//I added a set timeout here which is what you need
setTimeout(showbox, 30000); // after 5 secs
//document.write(' Your Message Goes Here And You See It Only Once ');
createCookie('visitedPreviously', 'visitedPreviously', 3); // 365 days persistence
}
//eraseCookie('visitedPreviously'); // FOR TEST PURPOSES
Remove the quote and the brackets in the "showbox()"
. Like this:
setTimeout(showbox, 30000);
I've corrected your code below, you had the setTimeout being called before the cookie had been checked!
function showbox() {
document.getElementById("apDiv1").style.visibility = "visible";
}
if (!readCookie('visitedPreviously')) {
setTimeout("showbox()", 30000); // after 30 seconds
//document.write(' Your Message Goes Here And You See It Only Once ');
createCookie('visitedPreviously', 'visitedPreviously', 3); // 365 days persistence
}
Please try it out.
You don't need quotes around showbox
:
setTimeout(showbox, 30000);
Docs: https://developer.mozilla/en-US/docs/Web/API/WindowTimers/setTimeout
You should modify your display code to:
<script type="text/javascript">
function showbox() {
document.getElementById("apDiv1").style.visibility = "visible";
}
setTimeout(showbox, 30000); // after 5 secs
</script>
<script type="text/javascript">
if (!readCookie('visitedPreviously')) {
//I added a set timeout here which is what you need
setTimeout(showbox, 30000); // after 5 secs
//document.write(' Your Message Goes Here And You See It Only Once ');
createCookie('visitedPreviously', 'visitedPreviously', 3); // 365 days persistence
}
//eraseCookie('visitedPreviously'); // FOR TEST PURPOSES
</script>
Please note the change is made above is:
//I added a set timeout here which is what you need
setTimeout(showbox, 30000); // after 5 secs
It is not a good practice to send a function as a string as you have done in the set timeout.
You need to modify your code to this
<script type="text/javascript">
function showbox() {
if (!readCookie('visitedPreviously')) {
document.getElementById("apDiv1").style.visibility = "visible";
//document.write(' Your Message Goes Here And You See It Only Once ');
createCookie('visitedPreviously', 'visitedPreviously', 3); // 365 days
}
//eraseCookie('visitedPreviously'); // FOR TEST PURPOSES
}
setTimeout(showbox, 30000); // after 5 secs
</script>
本文标签: javascriptShow Div After 30 SecStack Overflow
版权声明:本文标题:javascript - Show Div After 30 Sec - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745218919a2648300.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论