Skip to content
WJunction - Webmaster Forum

HELP: Java countdown

Status
Not open for further replies.
Hi,

I'm not familiar with JAVA so I'm hoping someone can help me with this. I need to have a countdown script that will display the amount of days/hours/minutes/seconds remaining. PHP will deliver a variable ($seconds) with the amount of seconds.

Thanks in advance.
 

4 comments

You need code in Javascript or php ?

I have to check my skool note book..have done this in skool project
 
javascript I suppose, it has to count down automatically. I found this, but I have no clue how to use it, can someone instruct me?
 
Just read in shout box that you have css

copy this part into your css section of HTML
#txt {
border:none;
font-family:Arial; <=== you can change font style
font-size:20pt; <== change the font size watever size u want
font-weight:bold; <== bold italic watevre u choose
border-right-color:#FFFFFF <== color code
}

For time clock
set 2..copy and paste this in your javascipt

var mins
var secs;

function cd() {
mins = 1 * m("10"); // <= u can change minutes here
secs = 0 + s(":01"); // <= u can change seconds here (always add an additional second to your total)
redo();
}

function m(obj) {
for(var i = 0; i < obj.length; i++) {
if(obj.substring(i, i + 1) == ":")
break;
}
return(obj.substring(0, i));
}

function s(obj) {
for(var i = 0; i < obj.length; i++) {
if(obj.substring(i, i + 1) == ":")
break;
}
return(obj.substring(i + 1, obj.length));
}

function dis(mins,secs) {
var disp;
if(mins <= 9) {
disp = " 0";
} else {
disp = " ";
}
disp += mins + ":";
if(secs <= 9) {
disp += "0" + secs;
} else {
disp += secs;
}
return(disp);
}

function redo() {
secs--;
if(secs == -1) {
secs = 59;
mins--;
}
document.cd.disp.value = dis(mins,secs); //<== if you want to setup additional displays here.
if((mins == 0) && (secs == 0)) {
window.alert("Time is up. Press OK to continue."); // <== you can put ur timeout message as want but in the ()
// window.location = "yourpage.htm" //<== this redirects to specified page once timer ends and ok button is hit
} else {
cd = setTimeout("redo()",1000);
}
}

function init() {
cd();
}
window.onload = init;
Once you copy the above post...Just paste it in notepad and save as countdown.js

Now...Copy this code in the head of your html page

<script type="text/javascript" src="countDown.js"></script>

example <head> <script type="text/javascript" src="countDown.js"></script>

</head>

copy this code into ur html body section

<form name="cd">
<input id="txt" readonly="true" type="text" value="10:00" border="0" name="disp">
</form>

example <body> above line
This will alert user with pop up once timer end and if you give redirect path..it will take the user to your given link once they hit Ok button in pop up alert...you can omit that [part if you dunt need.
Hope this help..Let me know if any error occurs


P.S : The text after //<== .... is just for instruction ..if you can delete them when you paste in notepad

for ex : window.alert("Time is up. Press OK to continue."); // <== you can put ur timeout message as want but in the ()

Delete from this line : // <== you can put ur timeout message as want but in the ()

Make sure you upload your countdown.js file on ur host.

excuse me : I am not English native.
 
Last edited:
You can use JQuery and a plugin to do the countdown JS.

Pass the seconds in by PHP by putting it in the call, e.g.
Code:
someDate.setSeconds(<?php echo $seconds; ?>);

Assuming the script containing $seconds is the same one which outputs the page you want to show the countdown on.

See here:
http://jsfiddle.net/8UYZP/1/

Note: in this example, I assumed $seconds its how many seconds into the future. Though you probably mean it's a unix timestamp of a date in the future.

FYI the comment in the code says one hour ahead, but its actually one day, I changed it last minute.
 
Last edited:
Status
Not open for further replies.

About the author

CuraHack
Banned · Joined
1,007
Messages
221
Reactions
63
Points
Grow your business

Advertise on WJunction

Connect with an established international community of webmasters, hosting providers, developers and online entrepreneurs.

  • Premium & Premium+ Sticky
  • Dedicated section banners
  • Site-wide banner advertising
Explore advertising options
Back
Top Bottom