www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Client-Side Development > JavaScript

    JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...)

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1  
    Old 11-04-2009, 10:27 AM
    Frico Frico is offline
    Registered User
     
    Join Date: Nov 2009
    Posts: 5
    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>
    Greetz,
    Frico
    Reply With Quote
      #2  
    Old 11-04-2009, 10:56 AM
    Kor's Avatar
    Kor Kor is offline
    Red Devil Moderator
     
    Join Date: Dec 2003
    Location: Bucharest, ROMANIA
    Posts: 11,142
    Quote:
    Originally Posted by Frico View Post
    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...
    Of course. The delay argument is in milliseconds, 1000 milliseconds=1 second.
    Reply With Quote
      #3  
    Old 11-04-2009, 10:58 AM
    Frico Frico is offline
    Registered User
     
    Join Date: Nov 2009
    Posts: 5
    Yes but I want to display it immediatly because if it will look weird if the time appears only after a second...
    Reply With Quote
      #4  
    Old 11-04-2009, 11:29 AM
    criterion9 criterion9 is offline
    Registered User
     
    Join Date: Jan 2009
    Posts: 981
    setInterval is to add a delay. Don't use it if you don't want a delay.
    Reply With Quote
      #5  
    Old 11-04-2009, 11:31 AM
    Frico Frico is offline
    Registered User
     
    Join Date: Nov 2009
    Posts: 5
    Yes but without setInterval it wont continue automaticly
    Reply With Quote
      #6  
    Old 11-04-2009, 11:34 AM
    criterion9 criterion9 is offline
    Registered User
     
    Join Date: Jan 2009
    Posts: 981
    Remove this:
    Quote:
    setInterval("displaytime()", 1000);
    Replace with this:
    Code:
    displaytime();
    See, just remove the setInterval.

    Last edited by criterion9; 11-04-2009 at 11:36 AM.
    Reply With Quote
      #7  
    Old 11-04-2009, 11:35 AM
    Frico Frico is offline
    Registered User
     
    Join Date: Nov 2009
    Posts: 5
    Well what I said in my previous post, that it wont continue automaticly...
    Reply With Quote
      #8  
    Old 11-04-2009, 11:36 AM
    criterion9 criterion9 is offline
    Registered User
     
    Join Date: Jan 2009
    Posts: 981
    What is the contents of your file now?
    Reply With Quote
      #9  
    Old 11-04-2009, 11:50 AM
    Frico Frico is offline
    Registered User
     
    Join Date: Nov 2009
    Posts: 5
    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 -->
    Not very interesting...
    Reply With Quote
      #10  
    Old 11-04-2009, 12:13 PM
    Drackir Drackir is offline
    Registered User
     
    Join Date: Nov 2009
    Posts: 3
    Change:
    Code:
    window.onload = function(){
    	setInterval("displaytime()", 1000);
    }
    To:
    Code:
    window.onload = function(){
    	displaytime();
    	setInterval("displaytime()", 1000);
    }
    This will output the time and then start the interval timer.
    Reply With Quote
      #11  
    Old 11-04-2009, 12:24 PM
    Drackir Drackir is offline
    Registered User
     
    Join Date: Nov 2009
    Posts: 3
    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())?>');
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    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
    Forum Jump


    All times are GMT -5. The time now is 12:19 AM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.