• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 881
  • Last Modified:

Hide div and show div after 15 minutes automatically without page refresh using javascript

I have a Hide/show link inside <div> tag. Url will hide the <div> tag.
<div> tag will be visible automatically after 15 minutes.

Would you please provide the javascript code that will do this. Below code will hide the <div>

Problem: I dont know how to show <div> after 15 minutes automatically without page refeesh javascript
function SetCookie() {
	if (document.cookie.length > 0)
	{
		myCookie = document.cookie.indexOf("MsgCookie");
		if (myCookie != -1) {
			document.getElementById('Msg').style.visibility = 'hidden';		
		}
	}
}
 
function DismissMsg()
{
    document.cookie = "MsgCookie";
	document.getElementById('Msg').style.visibility = 'hidden';	
 
}

Open in new window

0
Saroj13
Asked:
Saroj13
  • 4
  • 4
1 Solution
 
numberkruncherCommented:
You can automatically run your "DismissMsg" function after a set timeout:
setTimeout(DismissMsg, 1000*60*15);

Open in new window

0
 
numberkruncherCommented:
Just to clarify, your source would be changed to the following:
function SetCookie() {
        if (document.cookie.length > 0)
        {
                myCookie = document.cookie.indexOf("MsgCookie");
                if (myCookie != -1) {
                        document.getElementById('Msg').style.visibility = 'hidden';             
                }
        }
}
 
function DismissMsg()
{
    document.cookie = "MsgCookie";
        document.getElementById('Msg').style.visibility = 'hidden';     
 
}
 
// This will call "DismissMsg" once after 15 mins.
setTimeout(DismissMsg, 1000*60*15);

Open in new window

0
 
djon2003Commented:
Just to do a little correction.. The code above will hide the box after minutes.. and in the question is showing.. so :
function ShowHideMsg(showing) // Old DismissMsg
{
   if (showing == "undefined") showing = true;
 
    document.cookie = "MsgCookie";
        document.getElementById('Msg').style.visibility = showing ? 'visible' : 'hidden';     
 
}
 
// This will call "ShowHideMsg" once after 15 mins.
setTimeout(ShowHideMsg, 1000*60*15);
 
//Code to hide : ShowHideMsg(false);
//Code to show : ShowHideMsg(true);

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
numberkruncherCommented:
Sorry, my mistake, I misread the question.
0
 
Saroj13Author Commented:
Thanks for the responses.
I have put the complete code. Please check if its correct or not.Currently, Its not working.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Untitled Page</title>
    <script>
    window.onload=function()
{
    SetCookie();
    setTimeout(ShowHideMsg, 1000*60*1);
 
}
 
function SetCookie() {
	if (document.cookie.length > 0)
	{
		myCookie = document.cookie.indexOf("MsgCookie");
		if (myCookie != -1) {
			document.getElementById('Msg').style.visibility = 'hidden';		
		}
	}
}
 
 
 
function ShowHideMsg(showing) // Old DismissMsg
{
   if (showing == "undefined") showing = true;
 
    document.cookie = "MsgCookie";
        document.getElementById('Msg').style.visibility = showing ? 'visible' : 'hidden';     
 
}
    
    
    </script>
</head>
<body>
<div id="Msg" >
	<a href="#"  style="color:#688FCF; font-size:1.1em" onClick="ShowHideMsg(true);" >Show/Hide Div</a>
</div>
</body>
</html>

Open in new window

0
 
Saroj13Author Commented:
Please make the changes in the below code as soon as possible.

1. Show/Hide Div link will hide the link first.

2. After 15 minutes, div tag will become visible again without page refresh

Thanks
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Untitled Page</title>
    <script>
    window.onload=function()
{
    SetCookie();
    setTimeout(ShowHideMsg, 1000*60*15);
 
}
 
function SetCookie() {
	if (document.cookie.length > 0)
	{
		myCookie = document.cookie.indexOf("MsgCookie");
		if (myCookie != -1) {
			document.getElementById('Msg').style.visibility = 'hidden';		
		}
	}
}
 
 
 
function ShowHideMsg(showing) // Old DismissMsg
{
   if (showing == "undefined") showing = true;
 
    document.cookie = "MsgCookie";
        document.getElementById('Msg').style.visibility = showing ? 'visible' : 'hidden';     
 
}
    
    
    </script>
</head>
<body>
<div id="Msg" >
	<a href="#"  style="color:#688FCF; font-size:1.1em" onClick="ShowHideMsg(true);" >Show/Hide Div</a>
</div>
</body>
</html>

Open in new window

0
 
Saroj13Author Commented:
Hello Guys,

Please see the code if its fine or not...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Untitled Page</title>
    <script>
    window.onload=function()
{
    SetCookie();
    setTimeout(ShowHideMsg, 1000*60*15);
 
}
 
function SetCookie() {
	if (document.cookie.length > 0)
	{
		myCookie = document.cookie.indexOf("MsgCookie");
		if (myCookie != -1) {
			document.getElementById('Msg').style.visibility = 'hidden';		
		}
	}
}
 
 
 
function ShowHideMsg(showing) // Old DismissMsg
{
   if (showing == "undefined") showing = true;
 
    document.cookie = "MsgCookie";
        document.getElementById('Msg').style.visibility = showing ? 'visible' : 'hidden';     
 
}
    
    
    </script>
</head>
<body>
<div id="Msg" >
	<a href="#"  style="color:#688FCF; font-size:1.1em" onClick="ShowHideMsg(true);" >Show/Hide Div</a>
</div>
</body>
</html>
 
Open in New Window Select All 

Open in new window

0
 
numberkruncherCommented:
There is a syntax error in your above code, your missing a semi-colon after the onload event handler.

Also, you have some text "Open in New Window Select All" which appears below the closing html tag. You cannot have text below the closing html tag.

Just to clarify, "setTimeout" will execute a block of code once after the specified period. If you want code to run every 15 minutes, then you would need to use "setInterval" instead.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Untitled Page</title>
    <script>
    window.onload = function() {
       SetCookie();
       setTimeout(ShowHideMsg, 1000*60*15);
    };
 
    function SetCookie() {
       if (document.cookie.length > 0)
       {
          myCookie = document.cookie.indexOf("MsgCookie");
          if (myCookie != -1) {
             document.getElementById('Msg').style.visibility = 'hidden';             
          }
       }
    }
 
    function ShowHideMsg(showing) { // Old DismissMsg
       if (showing == "undefined") showing = true;
 
       document.cookie = "MsgCookie";
       document.getElementById('Msg').style.visibility = showing ? 'visible' : 'hidden';
    }
    </script>
</head>
<body>
<div id="Msg" >
        <a href="#"  style="color:#688FCF; font-size:1.1em" onClick="ShowHideMsg(true);" >Show/Hide Div</a>
</div>
</body>
</html>

Open in new window

0
 
Saroj13Author Commented:
If you want code to run every 15 minutes, I have used 15 minutes. i want to show <div> tag every 15 min..
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now