(Example Uses)
m (Related Blocks: Clarified versions)
 
(24 intermediate revisions by 13 users not shown)
Line 1: Line 1:
[[File:The timer.png|thumb|94px|An image of the timer on the project display.]]The '''Timer''' is a feature in [[Scratch]] that will record how much time, in seconds, have passed since the last time a [[project]] was opened or the timer was reset.
+
{{April Fools}}{{about|the value|more information on the block that reports this value|Timer (block)}}
 +
[[File:The timer.png|thumb|94px|An image of the timer on the project display.]]
 +
The '''Timer''' is a feature in [[Scratch]] that records how much time, in seconds, have passed since the last time the [[Green Flag|green flag]] was clicked or the timer was reset.
 +
 
 +
The timer is extremely accurate and the value itself is not affected by lag.{{citation needed|date=June 2016}}
  
The timer is extremely accurate and the value itself is not affected by lag. The timer will accumulate only one full day of error after approximately 185 years.{{citation needed}}
 
 
==Related Blocks==
 
==Related Blocks==
[[File:Timer related blocks.png|thumb|The two blocks related to the timer.]]The timer is associated with two [[blocks]], both found in the [[Sensing Blocks|Sensing]] category:
+
[[File:Timer related blocks.png|thumb|80px|The two blocks related to the timer.]]The timer is associated with two [[blocks]], both found in the [[Sensing Blocks|Sensing]] category:
*[[Reset Timer (block)|Reset Timer]]: Sets the timer's value to 0.0
+
* [[Reset Timer (block)|<sb>reset timer</sb>]]: Sets the timer's value to 0.0
*[[Timer (block)|Timer]]: The [[Stage Monitor|stage monitor]] and [[Reporter Block|reporter block]] of the value
+
* [[Timer (block)|<sb>(timer)</sb>]]: The [[Stage Monitor|stage monitor]] and [[Reporter Block|reporter block]] of the value
 +
 
 +
Starting from [[Scratch 2.0]], this block is also available:
 +
* [[When () is greater than () (block)|<sb>when [timer v] > (10)</sb>]]: This starts when timer value is greater than the input value.
 +
 
 
==How it works==
 
==How it works==
 
The timer works in the following ways:
 
The timer works in the following ways:
*Continuously counts upwards by tenths of seconds
+
* Continuously counts upwards by tenths of seconds
*Cannot be paused or stopped (that would imply pausing or stopping the flow of time itself)
+
* Cannot be paused or stopped (that would imply pausing or stopping the flow of time itself)
*Shows seconds, not minutes or more {{-}} even when the current value is longer than sixty seconds
+
* Shows seconds, not minutes or larger units, even when the current value is sixty seconds or longer
*Resets only when a project is opened inside Scratch or by execution of the Reset Timer block
+
* Resets only when the [[Green Flag]] is clicked or by execution of the Reset Timer block
*Continues even when its project stops running
+
* Continues even when the project stops running
  
==Other Method==
+
==Alternatives==
A lot of [[Scratcher]]s prefer to use a [[variable]] instead of the timer, because the timer does not stop when the [[Stop Sign|stop button]] is pressed. This method, however, is just over 7% slower than the real timer. {{citation needed}}
+
One workaround is to increment a "timer" variable by 1/30th every frame; since Scratch runs at 30 [[Frames-per-second|frames per second]], this can approximate an increase of 1 per second.
 +
<scratchblocks>
 +
when gf clicked
 +
set [timer v] to [0]
 +
forever
 +
    wait (0) secs
 +
    change [timer v] by ((1) / (30))
 +
</scratchblocks>
 +
This is, however, slightly less accurate than the timer, especially if the frame rate drops below 30 fps, and can be affected by other scripts.<ref>https://scratch.mit.edu/projects/114798177/</ref>
  
 +
Another workaround involves the [[Days Since 2000 (block)|Days Since 2000 block]], which is itself a highly precise timer.
 
<scratchblocks>
 
<scratchblocks>
 
when gf clicked
 
when gf clicked
set [timer v] to (0)
+
set [timestamp v] to (days since 2000)
 
forever
 
forever
    wait (1) secs
+
set [timer v] to ((86400) * ((days since 2000) - (timestamp)))
    change [timer v] by (1)
 
 
</scratchblocks>
 
</scratchblocks>
 
+
The drawback to both of these workarounds is that the actual timer continues to increase after the project has been stopped, while these scripts do not.
The accuracy can be improved slightly by reducing the wait by a few milliseconds, for example, to 0.99 seconds.
 
  
 
==Example Uses==
 
==Example Uses==
 
The timer is commonly used in projects that require a clock of some sort, such as racing projects. Other uses include:
 
The timer is commonly used in projects that require a clock of some sort, such as racing projects. Other uses include:
*Using time duration in [[One Sprite One Script Project|one sprite one script projects]] without pausing the [[script]]
+
* Using time duration in [[One Sprite One Script Project|one sprite one script projects]] without pausing the [[script]]
*Checking how long a level took to complete
+
* Checking how long a level took to complete
* A lag detector for a game.
+
* A [[lag]] detector for a game
* A turbo mode detector.
+
* A [[Turbo Mode]] detector
 +
* A project stop detector
 +
 
 +
==References==
 +
<references/>
 +
 
 
[[Category:Values]]
 
[[Category:Values]]
 
[[de:Stoppuhr]]
 
[[de:Stoppuhr]]

Latest revision as of 15:04, 16 April 2020

This article is about the value. For more information on the block that reports this value, see Timer (block).
An image of the timer on the project display.

The Timer is a feature in Scratch that records how much time, in seconds, have passed since the last time the green flag was clicked or the timer was reset.

The timer is extremely accurate and the value itself is not affected by lag.[citation needed]

Related Blocks

The two blocks related to the timer.
The timer is associated with two blocks, both found in the Sensing category:

Starting from Scratch 2.0, this block is also available:

How it works

The timer works in the following ways:

  • Continuously counts upwards by tenths of seconds
  • Cannot be paused or stopped (that would imply pausing or stopping the flow of time itself)
  • Shows seconds, not minutes or larger units, even when the current value is sixty seconds or longer
  • Resets only when the Green Flag is clicked or by execution of the Reset Timer block
  • Continues even when the project stops running

Alternatives

One workaround is to increment a "timer" variable by 1/30th every frame; since Scratch runs at 30 frames per second, this can approximate an increase of 1 per second.

when gf clicked
set [timer v] to [0]
forever
    wait (0) secs
    change [timer v] by ((1) / (30))

This is, however, slightly less accurate than the timer, especially if the frame rate drops below 30 fps, and can be affected by other scripts.[1]

Another workaround involves the Days Since 2000 block, which is itself a highly precise timer.

when gf clicked
set [timestamp v] to (days since 2000)
forever
set [timer v] to ((86400) * ((days since 2000) - (timestamp)))

The drawback to both of these workarounds is that the actual timer continues to increase after the project has been stopped, while these scripts do not.

Example Uses

The timer is commonly used in projects that require a clock of some sort, such as racing projects. Other uses include:

References

  1. https://scratch.mit.edu/projects/114798177/