|
|||||||
| JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...) |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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: HTML Code:
//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> Frico |
|
#2
|
||||
|
||||
|
Of course. The delay argument is in milliseconds, 1000 milliseconds=1 second.
|
|
#3
|
|||
|
|||
|
Yes but I want to display it immediatly because if it will look weird if the time appears only after a second...
|
|
#4
|
|||
|
|||
|
setInterval is to add a delay. Don't use it if you don't want a delay.
|
|
#5
|
|||
|
|||
|
Yes but without setInterval it wont continue automaticly
|
|
#6
|
|||
|
|||
|
Remove this:
Quote:
Code:
displaytime(); Last edited by criterion9; 11-04-2009 at 11:36 AM. |
|
#7
|
|||
|
|||
|
Well what I said in my previous post, that it wont continue automaticly...
|
|
#8
|
|||
|
|||
|
What is the contents of your file now?
|
|
#9
|
|||
|
|||
|
HTML Code:
<!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 --> |
|
#10
|
|||
|
|||
|
Change:
Code:
window.onload = function(){
setInterval("displaytime()", 1000);
}
Code:
window.onload = function(){
displaytime();
setInterval("displaytime()", 1000);
}
|
|
#11
|
|||
|
|||
|
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())?>');
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|