/    Sign up×
Community /Pin to ProfileBookmark

setInterval()

Hello,
I need help with setInterval()! Well I have this code to show the current time.
But when I set the interval to 1000 it will take 1 second before it shows the time… This is my code:

[code=html]//Javascript code:
<script type=”text/javascript”>
var serverdate = new Date()
function padlength(string) {
var output = (string.toString().length==1)? “0”+string : string
return output
}

function displaytime() {
serverdate.setSeconds(serverdate.getSeconds()+1)
var timestring = padlength(serverdate.getHours())+”:”+padlength(serverdate.getMinutes())+”:”+padlength(serverdate.getSeconds())
document.getElementById(“servertime”).innerHTML = timestring
}

window.onload = function(){
setInterval(“displaytime()”, 1000);
}
</script>
// HTML part for showing the time:
<a href=”index.php?a=mod_stat_servertime”>Server Time: <span class=”servertime” id=”servertime”></span></a>[/code]

Greetz,
Frico

to post a comment
JavaScript

10 Comments(s)

Copy linkTweet thisAlerts:
@KorNov 04.2009 — Hello,

I need help with setInterval()! Well I have this code to show the current time.

But when I set the interval to 1000 it will take 1 second before it shows the time... [/QUOTE]

Of course. The delay argument is in milliseconds, 1000 milliseconds=1 second.
Copy linkTweet thisAlerts:
@FricoauthorNov 04.2009 — Yes but I want to display it immediatly because if it will look weird if the time appears only after a second...
Copy linkTweet thisAlerts:
@criterion9Nov 04.2009 — setInterval is to add a delay. Don't use it if you don't want a delay.
Copy linkTweet thisAlerts:
@FricoauthorNov 04.2009 — Yes but without setInterval it wont continue automaticly
Copy linkTweet thisAlerts:
@criterion9Nov 04.2009 — Remove this:

setInterval("displaytime()", 1000);
[/quote]

Replace with this:
<i>
</i>displaytime();


See, just remove the setInterval.
Copy linkTweet thisAlerts:
@FricoauthorNov 04.2009 — Well what I said in my previous post, that it wont continue automaticly...
Copy linkTweet thisAlerts:
@criterion9Nov 04.2009 — What is the contents of your file now?
Copy linkTweet thisAlerts:
@FricoauthorNov 04.2009 — [code=html]<!DOCTYPE html PUBLIC "-//W3C// DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-tranisitional.dtd">
<html xmlns="httpd://www.w3.org/1999/xhtml" xml:lang="nl" lang="nl">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title><?php echo $paginatitel; ?></title>
<link href="./includes/style.css" rel="stylesheet" type="text/css" />
</head>
<body style="#000000">
<script type="text/javascript">
var serverdate = new Date()
function padlength(string) {
var output = (string.toString().length==1)? "0"+string : string
return output
}

function displaytime() {
serverdate.setSeconds(serverdate.getSeconds()+1)
var timestring = padlength(serverdate.getHours())+":"+padlength(serverdate.getMinutes())+":"+padlength(serverdate.getSeconds())
document.getElementById("servertime").innerHTML = timestring
}

window.onload = function(){
setInterval("displaytime()", 1000);
}
</script>
<div id="container">
<div id="header">
<h1>Big Business Mafia</h1>
</div>
<div id="leftsidebar">
<ul id="leftnavlijst">
<li><h2>Profile</h2></li>
<li><a href="index.php?a=mod_prof_main">Main</a></li>
<li><a href="index.php?a=mod_prof_profile">My Profile</a></li>
<li><a href="index.php?a=mod_prof_messages">Messages</a></li>
<div class="last">
<li><a href="index.php?a=mod_prof_logout">Logout</a></li>
</div>
<li><h2>Family</h2></li>
<li><a href="index.php?a=mod_fam_familys">Family's</a></li>
<li><a href="index.php?a=mod_fam_createfam">Create Family</a></li>
<li><a href="index.php?a=mod_fam_famprofile">Family: <?php echo $family; ?></a></li>
<div class="last">
<li><a href="index.php?a=mod_fam_joinfam">Join Family</a></li>
</div>
<li><h2>Locations</h2></li>
<li><a href="index.php?a=mod_loc_bank">Bank</a></li>
<li><a href="index.php?a=mod_loc_travel">Travel</a></li>
<li><a href="index.php?a=mod_loc_shop">Shop</a></li>
<li><a href="index.php?a=mod_loc_myhome">My Home</a></li>
<li><a href="index.php?a=mod_loc_jail">Jail</a></li>
<li><a href="index.php?a=mod_loc_gym">Gym</a></li>
<li><a href="index.php?a=mod_loc_hospital">Hospital</a></li>
<div class="last">
<li><a href="index.php?a=mod_loc_garage">Garage</a></li>
</div>
</ul>
</div>
<div id="rightsidebar">
<ul id="rightnavlijst">
<li><h2>Crimes</h2></li>
<li><a href="index.php?a=mod_crim_crimes">Local Crimes</a></li>
<li><a href="index.php?a=mod_crim_oc">Organised Crime</a></li>
<li><a href="index.php?a=mod_crim_cartheft">Car Theft</a></li>
<li><a href="index.php?a=mod_crim_drugs">Drugs</a></li>
<div class="last">
<li><a href="index.php?a=mod_crim_booze">Booze</a></li>
</div>
<li><h2>Casino</h2></li>
<li><a href="index.php?a=mod_cas_roulette">Roulette</a></li>
<li><a href="index.php?a=mod_cas_blackjack">Blackjack</a></li>
<li><a href="index.php?a=mod_cas_fruitmachine">Fruit Machine</a></li>
<div class="last">
<li><a href="index.php?a=mod_cas_lottery">Lottery</a></li>
</div>
<li><h2>Other</h2></li>
<li><a href="index.php?a=mod_stat_servertime">Server Time: <span class="servertime" id="servertime"></span></a></li>
<li><a href="index.php?a=mod_stat_memberlist">Memberlist</a></li>
<li><a href="index.php?a=mod_stat_searchmembers">Search Members</a></li>
<div class="last">
<li><a href="index.php?a=mod_stat_wanted">Wanted</a></li>
</div>
<ul>
</div>
<div id="content">

<!-- Einde Header -->[/code]


Not very interesting...
Copy linkTweet thisAlerts:
@DrackirNov 04.2009 — Change:
[CODE]window.onload = function(){
setInterval("displaytime()", 1000);
}[/CODE]

To:
[CODE]window.onload = function(){
displaytime();
setInterval("displaytime()", 1000);
}[/CODE]


This will output the time and then start the interval timer.
Copy linkTweet thisAlerts:
@DrackirNov 04.2009 — Oh and just so you know, Javascript doesn't get the date/time from the server, it gets it from the client's computer. If you want the actual time as it is on the server, you'll have to seed the date function using PHP. Try something like this (not tested):
[CODE]var serverdate = new Date('<? print date("F d, Y H:i:s", time())?>');[/CODE]
×

Success!

Help @Frico 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.7,
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,
)...