/    Sign up×
Community /Pin to ProfileBookmark

How to make a countdown timer stop when Internet Connection is lost

Hello all,

I have a javascript code with a countdown timer that runs on the client side and is updated regularly to the date set in mysql db.

I want to make the timer stop when the internet connection is lost.

Any ideas ? I am thinking on using clearTimeout()…but I need to associate it with the internet connection.

Here is the script:

function countdown(vid)
{
var acao;
var divs;

Timer=Timer+1;

if((Timer%10==”0″)||(Timer==”1″)){
$.get(“current.php”, {
id:vid},
function(data){
acao=data.split(“||”);
for(n=0;n<=acao.length;n++)
{
if(acao[n] != undefined)
{
divis=acao[n].split(“##”);
$(‘#futu’+divis[0]).html(divis[1]);
}}
});
}
var cvid=”vid”;
var tacao=vid.split(“|”);
for(i=0;i<=tacao.length;i++)
{
if(tacao[i] != undefined)
{
var dd=$(‘#futu’+tacao[i]).text();
var paus=$(‘#pause’+tacao[i]).text();

var cdd=dd-1;
$(‘#futu’+tacao[i]).html(cdd);

dd=dd*1000;
dday=Math.floor(dd/(60*
60*1000*24)*1)
dhour=Math.floor((dd%(60*
60*1000*24))/(60*60*1000)*1)
dmin=Math.floor(((dd%(60*
60*1000*24))%(60*60*1000))/(60*1000)*1)
dsec=Math.floor((((dd%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1)

if(dday==0&&dhour==0&&dmin==0&&dsec==0){
$(‘#Us’+tacao[i]).html(“Vend”);
//return
}
if(dhour <=9)
{
dhour = “0”+dhour;
}
if(dmin <=9)
{
dmin = “0”+dmin;
}
if(dsec <=9)
{
dsec = “0”+dsec;
}
if(paus==’yes’)
{
var valll=”Paused”;
}
if(dd>=0)
{
var valll=dhour+”:”+dmin+”:”+dsec;
}
if(dd<0)
{
var valll=”End”;
}
if(dd<11000)
{
var valll=”<font color=red>”+valll+”</font>”;
}

$(‘#Us’+tacao[i]).html(valll);
}}
refreshID=setTimeout(“countdown(‘”+vid+”‘)”,1000);
}

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@jalarieFeb 11.2011 — For each loop of your counter, try to load an image that you KNOW will be available through the internet connection. Include an "onerror='SomeFunction();'" attribute and kill the timer in SomeFunction.
Copy linkTweet thisAlerts:
@tirnaFeb 11.2011 — If the image has been cached by the browser, would the browser simply reload the cached image without trying to reload the original from the server?

To eliminate this possibility append a unique query string, like the current date and time, to the url of the image which will force the browser to try to download the image from the server every time.
Copy linkTweet thisAlerts:
@mirza_yasir4Feb 12.2011 — For each loop of your counter, try to load an image that you KNOW will be available through the internet connection. Include an "onerror='SomeFunction();'" attribute and kill the timer in SomeFunction.[/QUOTE]

This could be the right solution I guess.
×

Success!

Help @lones spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...