Current location: Hot Scripts Forums » General Web Coding » JavaScript » Countdown Timer


Countdown Timer

Reply
  #1 (permalink)  
Old 10-06-05, 04:09 PM
Makien Makien is offline
Newbie Coder
 
Join Date: Oct 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Countdown Timer

This is a scirpt I got off line. It's almost exactly what I need but with one problem: the countdown timer stops if you reload, or leave the page. I tried to e-mail the person who wrote it but I get the e-mail back saying e-mail no longer in service.
What I need it to do is keep the time going weather your on the page or not. Like if I put in 28 hours and I come back 12 hours later (after computer was off) it's still counting down at 16 hours. Thankx for any help.
Code:
-----------------script----------------
if(navigator.userAgent.match(/(?!Opera)(MSIE)|(Gecko)/))new function()
{
var DOM_TARGET="target";

var TARGET=window.attachEvent?"srcElement": DOM_TARGET;
var MAX_TICKS=60*60*99+60*59+59;
var ORIG_TITLE=document.title;
var DLG_URL="";
var IE_DLG_STYLE="center:yes; dialogWidth:325px; dialogHeight:175px; status:no";
var GK_DLG_STYLE="dependent,width=325,height=175";
var COUNT_DOWN="CH_dtimer";
var COUNT_UP="CH_utimer";
var clockList=[];


function addListener(e,ev,listener)
{
if(window.attachEvent)
{
e.attachEvent("on"+ev,listener);
}
else
{
e.addEventListener(ev,listener,false);
}
}

function resetHandler(clock)
{
clock.reset();clock.eReset.disabled=true;clock.ePause.disabled=true;clock.eResume.disabled=true;clock.eStart.disabled=false;
if(clock.type==COUNT_DOWN)
{
clock.disableTf(false);clock.eMinutes.focus();
}
}

function initHandlers(clock)
{
function handler(event)
{
switch(event[TARGET])
{
case clock.ePause:clock.stop();clock.eResume.disabled=false;clock.eResume.focus();
clock.ePause.disabled=true;
break;
case clock.eResume:clock.start();
clock.eResume.disabled=true;
clock.eReset.focus();
clock.ePause.disabled=false;
break;
case clock.eReset:resetHandler(clock);
break;
case clock.eStart:if(clock.eStart.disabled)return;
default:function getNumberInput(e)
{
var s=e.value;
if(!s.length)
{
return 0;
}
var i=parseInt(e.value);
if(isNaN(i)){switch(e)
{
case clock.eHours:alert($UMSG_INVALID_HOURS);
break;
case clock.eMinutes:alert($UMSG_INVALID_MINUTES);
break;
case clock.eSeconds:alert($UMSG_INVALID_SECONDS);
}
e.value="";e.focus();
}
else if(s.length!=new String(i).length)
{
e.value=new String(i);
}
return i;
}
if(clock.type==COUNT_UP)
{
clock.eStart.disabled=true;
clock.ePause.disabled=false;
clock.eReset.disabled=false;
clock.eReset.focus();
clock.start();
return;
}
var hrs=getNumberInput(clock.eHours);
if(isNaN(hrs))
{
return;
}
var mins=getNumberInput(clock.eMinutes);
if(isNaN(mins))
{
return;
}
var secs=getNumberInput(clock.eSeconds);
if(isNaN(secs))
{
return;
}
if(hrs+mins+secs==0)
{
alert($UMSG_NO_INPUT);
return;
}
if(mins>59)
{
alert($UMSG_INVALID_MINUTES);
clock.eMinutes.focus();
return;
}
if(secs>59)
{
alert($UMSG_INVALID_SECONDS);
clock.eSeconds.focus();
return;
}
clock.eStart.disabled=true;
clock.ePause.disabled=false;
clock.eReset.disabled=false;
if(event[TARGET]==clock.eStart)
{
clock.eReset.focus();
}
clock.disableTf(true);
clock.start(hrs*3600+mins*60+secs);
break;
}
}
addListener(clock.eStart,"click",handler);
addListener(clock.ePause,"click",handler);
addListener(clock.eResume,"click",handler);
addListener(clock.eReset,"click",handler);}

function initClock(clockType,number)
{
function getElement(id)
{
return document.getElementById(clockType+number+"_"+id);
}
if(!getElement("digits"))return false;
var clock=new Clock();
clockList[clockList.length]=clock;
function Clock()
{
this.type=clockType;
this.eCont=document.getElementById(clockType+number);
this.eDigits=getElement("digits");
this.eStart=getElement("start");
this.ePause=getElement("pause");
this.eResume=getElement("resume");
this.eReset=getElement("reset");
if(clockType==COUNT_DOWN)
{
this.eHours=getElement("hours");
this.eSeconds=getElement("seconds");
this.eMinutes=getElement("minutes");
this.eHours.maxLength=this.eMinutes.maxLength=this.eSeconds.maxLength=2;
function handler(event)
{
if(String.fromCharCode(event.keyCode)=="\r")
{
clock.eStart.click();
if(TARGET==DOM_TARGET)
{
return false;
}
else{event.returnValue=false;
}
}
}
addListener(this.eHours,"keydown",handler);
addListener(this.eMinutes,"keydown",handler);
addListener(this.eSeconds,"keydown",handler);
}
this.cTicks=0;
this.eDigits.innerHTML="00:00:00";
Clock.ticker=function()
{
if(clock.type==COUNT_UP)
{
++clock.cTicks;clock.showTime();
}
else
{
clock.showTime();
if(clock.cTicks)
{
--clock.cTicks;
}
else
{
clock.stop();
clock.ePause.disabled=true;
clock.eResume.disabled=true;
clock.eReset.disabled=true;
window.focus();
if(window.showModalDialog&&window.attachEvent){showModalDialog(DLG_URL,clockList,IE_DLG_STYLE);
}
else
{
window.open(DLG_URL,"",GK_DLG_STYLE).focus();
}
clock.eReset.disabled=false;
resetHandler(clock);clock.eReset.click();
}
}
};
this.showTime=function()
{
if(this.cTicks<=MAX_TICKS)
{
this.eDigits.innerHTML=Math.floor(this.cTicks/3600/10)+""+Math.floor(this.cTicks/3600%10)+":"+Math.floor(this.cTicks%3600/60/10)+""+Math.floor(this.cTicks%3600/60%10)+":"+Math.floor(this.cTicks%3600%60/10)+""+this.cTicks%3600%60%10;
document.title=this.eDigits.innerHTML+" "+ORIG_TITLE;
}
};
this.disableTf=function(flag)
{
this.eHours.disabled=this.eMinutes.disabled=this.eSeconds.disabled=flag;
};
this.start=function(newCountDownFrom)
{
if(newCountDownFrom)
{
this.cTicks=newCountDownFrom;this.showTime();
--this.cTicks;
}
this.intervalID=setInterval(Clock.ticker,1000);
};
this.stop=function()
{
clearInterval(this.intervalID);
this.intervalID=null;
};
this.reset=function()
{
this.stop();
this.cTicks=0;this.showTime();
};
this.isActive=function()
{
return this.intervalID!=null;
};
if(this.type==COUNT_DOWN)
{
this.disableTf(false);
}
this.eStart.disabled=false;
this.ePause.disabled=true;
this.eResume.disabled=true;
this.eReset.disabled=true;initHandlers(this);
}
return true;
}
for(var u=1;initClock(COUNT_UP,u);++u);
for(var d=1;initClock(COUNT_DOWN,d);++d);
for(var i=0;i<clockList.length;++i)
{
clockList[i].eCont.style.visibility="visible";
}
if(!window.$NO_CD_FOCUS&&d!=1)
{
clockList[u-1].eMinutes.focus();
}
}
--------end script--------
--------html---------
<table border="2">
<tr id="CH_dtimer1" class="timer">
<td><div class="title" class="field_cont" class="btn_cont">*name here*</div></td>
<td><div id="CH_dtimer1_digits" class="digits"></div></td>
<td class="timer_pad"><label class="field">Hours:<input id="CH_dtimer1_hours" class="hours" type="text" size="2" disabled="disabled" />
</label>

<label class="field">Minutes:<input id="CH_dtimer1_minutes" class="minutes" type="text" size="2" disabled="disabled" />
</label>

<label class="field">Seconds: <input id="CH_dtimer1_seconds" class="seconds" type="text" size="2" disabled="disabled" />
</label>
<input id="CH_dtimer1_start" class="start" type="button" value="Start" disabled="disabled" />
<input id="CH_dtimer1_pause" class="pause" type="button" value="Pause" disabled="disabled" />
<input id="CH_dtimer1_resume" class="resume" type="button" value="Resume" disabled="disabled" />
<input id="CH_dtimer1_reset" type="button" value="Reset" disabled="disabled" />
</div>
</td>
</tr>
</table>
--------------end html----------------

Last edited by Makien; 10-06-05 at 04:32 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 10-06-05, 04:58 PM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
To make this working to 100%, you need serverside code like PHP or ASP.
The reason is that Javascript runs clientside inside the browser.
Anything you want to keep alive even if the user goes offline, must be handled serverside, and it usually requires the user to log in so the server knows the current state for that specific user.

You could store the current value of the counter along with the date on browser exit, so that the script can check if there's already a counter running when initializing it. Then use the stored countdown value and the date to figure out the current value of the counter. However, if the user turns off cookies, or clears them, the counter will be reset back to the default value anyway.
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 10-06-05, 05:49 PM
Makien Makien is offline
Newbie Coder
 
Join Date: Oct 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
thanx, i have almost no knowledge of php oe asp... looks like i gotta start reading up
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Countdown Timer Question Dominate_Predator JavaScript 3 07-03-05 05:47 PM
auction countdown timer like in ebay edwient PHP 2 05-12-05 02:19 AM
countdown timer misterman JavaScript 1 03-22-05 02:35 PM
Need a countdown clock Tocpe Script Requests 1 10-17-04 09:38 AM
Day elapsed countdown. Jaeboy PHP 1 11-19-03 10:23 AM


All times are GMT -5. The time now is 10:25 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.