<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://wiki.spheredev.org/index.php?action=history&amp;feed=atom&amp;title=Legacy%3AGetTime</id>
		<title>Legacy:GetTime - Revision history</title>
		<link rel="self" type="application/atom+xml" href="http://wiki.spheredev.org/index.php?action=history&amp;feed=atom&amp;title=Legacy%3AGetTime"/>
		<link rel="alternate" type="text/html" href="http://wiki.spheredev.org/index.php?title=Legacy:GetTime&amp;action=history"/>
		<updated>2026-05-16T22:22:55Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.29.0</generator>

	<entry>
		<id>http://wiki.spheredev.org/index.php?title=Legacy:GetTime&amp;diff=587&amp;oldid=prev</id>
		<title>Apollolux: created from http://web.archive.org/web/20110802085151/http://www.spheredev.org/wiki/GetTime</title>
		<link rel="alternate" type="text/html" href="http://wiki.spheredev.org/index.php?title=Legacy:GetTime&amp;diff=587&amp;oldid=prev"/>
				<updated>2013-06-01T22:46:19Z</updated>
		
		<summary type="html">&lt;p&gt;created from http://web.archive.org/web/20110802085151/http://www.spheredev.org/wiki/GetTime&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Obtains the number of milliseconds elapsed since an arbitrary time.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
&lt;br /&gt;
{{Usage|returns=integer|func=GetTime}}&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
var time = GetTime();&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above gets the number of milliseconds passed since some predetermined time, and places it in the &amp;lt;var&amp;gt;time&amp;lt;/var&amp;gt; variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
var start_time = GetTime();&lt;br /&gt;
// Code that needs to be timed&lt;br /&gt;
// ...&lt;br /&gt;
var duration = GetTime() - start_time;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example will find out how long a particular piece of code runs for.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
function Delay(ms)&lt;br /&gt;
{&lt;br /&gt;
  var start = GetTime();&lt;br /&gt;
  while (GetTime() - start &amp;lt; ms);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above function is a simple mechanism to pause script execution for a given number of milliseconds, called using, for example, &amp;lt;code&amp;gt;Delay(1500);&amp;lt;/code&amp;gt;, which pauses script execution for one and a half seconds. No updating or redrawing will take place while the delay is occurring.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
var done = false;&lt;br /&gt;
var fps = GetFrameRate();&lt;br /&gt;
var updates_performed = 0;&lt;br /&gt;
var start_time = GetTime();&lt;br /&gt;
while (!done)&lt;br /&gt;
{&lt;br /&gt;
  while (updates_performed &amp;lt; (GetTime() - start_time) * 0.001 * fps)&lt;br /&gt;
  {&lt;br /&gt;
    // Update game here&lt;br /&gt;
    // ...&lt;br /&gt;
    updates_performed++;&lt;br /&gt;
  }&lt;br /&gt;
  // Draw the game screen here&lt;br /&gt;
  // ...&lt;br /&gt;
  FlipScreen();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example is one way to regulate the speed of a Sphere game inside a JavaScript loop. The game code will have to be separated into updates (player input processing, moving entities, checking collisions, etc.) and drawing (drawing sprites, the map, score, etc.).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
var FPS = 60;             // The desired FPS&lt;br /&gt;
var delayTime = 1000/FPS; // The time between frames&lt;br /&gt;
var lastTime = GetTime(); // The time of the last frame&lt;br /&gt;
while(loopCondition)      // While we should continue looping&lt;br /&gt;
{&lt;br /&gt;
  if(GetTime() &amp;gt; lastTime + delayTime) // If enough time has elapsed&lt;br /&gt;
  {&lt;br /&gt;
    lastTime = GetTime(); // Record the time of this frame&lt;br /&gt;
    doStuff();            // Do your stuff&lt;br /&gt;
  }&lt;br /&gt;
  drawStuff();            // Draw your stuff&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This example is an alternative to the one above. Obviously, doStuff() and drawStuff() will correspond with functions that actually process the game logic and draw the game state respectively.&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
*	The starting time can be assumed to be constant throughout the running of a game. Thus GetTime() is very useful as a tool to pace JavaScript loops, which would normally run at different speeds on different systems.&lt;br /&gt;
*	Core JavaScript provides a similar mechanism via the [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date Date] object's [https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getTime Date.getTime]() method. This counter, if used with a Date object that reflects the current time, displays the number of milliseconds since midnight 01 January, 1970 UTC. Refer to the links for more information.&lt;br /&gt;
*	Because GetTime() is a signed integer, it will wraparound from time to time.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
*	[[API:FlipScreen|FlipScreen]]()&lt;br /&gt;
*	[[API:GetFrameRate|GetFrameRate]]()&lt;br /&gt;
*	Core JavaScript [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date Date] object&lt;br /&gt;
*	Core JavaScript [https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getTime Date.getTime]()&lt;br /&gt;
&lt;br /&gt;
{{API:Script/navbox}}&lt;/div&gt;</summary>
		<author><name>Apollolux</name></author>	</entry>

	</feed>