[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Javascript reload page when countdown is 0

Posted on 2012-08-22
7
Medium Priority
?
487 Views
Last Modified: 2012-08-22
Hello experts.
I want to add a reload_page function when countdown is 0 to the  attached code.
Any help?
// how many minutes
var mins = 15;

// how many seconds (don't change this)
var secs = mins * 60;
function countdown() {
	setTimeout('Decrement()',1000);
}
function Decrement() {
	if (document.getElementById) {
		var minutes = document.getElementById("minutes");
		var seconds = document.getElementById("seconds");
		// if less than a minute remaining
		if (seconds < 59) {
			seconds.value = secs;
		} else {
			minutes.value = getminutes();
			seconds.value = getseconds();
			if (minutes.value == 0 && seconds.value == 0)
                    location.reload(true);
		}
		secs--;
        if(secs >= 0)
        {
         setTimeout('Decrement()',1000);
         }
	}
}
function getminutes() {
	// minutes is seconds divided by 60, rounded down
	mins = Math.floor(secs / 60);
	return mins;
}
function getseconds() {
	// take mins remaining (as seconds) away from total seconds remaining
	return secs-Math.round(mins *60);
}
window.onload=function() {
countdown();
}

Open in new window

0
Comment
Question by:Panos
  • 3
  • 3
7 Comments
 
LVL 9

Assisted Solution

by:BlueYonder
BlueYonder earned 1002 total points
ID: 38319972
All you really need is
<body onload=”javascript:setTimeout(“location.reload(true);”,timeperiod);”>

The other stuff does not seem necessary.  What are you trying to do?
0
 
LVL 2

Author Comment

by:Panos
ID: 38320003
I have this code on my login page. After 5 failed attemts i lock the login form for a couple of minutes showing a message with counting down minutes and seconds. In the end i want to reload the page to show again the form (the show hide function is working server side).
0
 
LVL 9

Expert Comment

by:BlueYonder
ID: 38320022
Here's an example of a timer.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 9

Accepted Solution

by:
BlueYonder earned 1002 total points
ID: 38320023
0
 
LVL 2

Assisted Solution

by:GhostScripter
GhostScripter earned 498 total points
ID: 38320031
The problem is that a JS can be bypassed fast. So if you want to lock the login form for a period of time you should better do this on the server side.

You can use a text file or database to store for example the request IP, the number of login attempts and the time of the last false login. Then if this IP tries to login again you count up the false login attempts. If the count reaches for e.g 5 you fill a field "locked" with true as long as the last false login is not older than X minutes / hours or whatever.
Maybe you can also give a feedback on the number of false logins and the reason why the login form is locked for a special period of time.
0
 
LVL 2

Author Comment

by:Panos
ID: 38320057
Hi GhostScripter.
I did write in my post  38320003 that the show hide function is working server side.

BlueYonder
The link you posted is a timer but with the needed function if (secs==0)
    {
        StopTheClock()

        alert("You have just wasted 10 seconds of your life.")
    }
Instead of alert i could reload the page.
But how can i use this with my elements that are showing the left minutes and seconds?

var minutes = document.getElementById("minutes");
var seconds = document.getElementById("seconds");
0
 
LVL 2

Author Comment

by:Panos
ID: 38320170
I noticed something else:
This part of my code is doing the job:
if (minutes.value == 0 && seconds.value == 0)
                    location.reload(true);

But before reloading i'm getting a pop up message from firefox that is asking me if i want to reload the page.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question