From Test-Scratch-Wiki
- This eng is about the block. For more information on the value this block holds, see Timer (value).
Timer | |
Category | Sensing |
Type | Reporter |
The Timer block is a Sensing block and a Reporter block. The block starts at 0 when Scratch is launched and increases gradually; every second it will have increased by 1. This block is almost always used with the Reset Timer block — usually the timer must be reset at the beginning of a project for the Timer block to hold the right value.
This block can be displayed as a Stage monitor.
Example Uses
- Tracking time duration in One Sprite One Script Projects instead of using the Wait () Secs block
— in One Sprite One Script projects, in order to keep it Single Frame (scroll to see the whole script)
when gf clicked set [PersonX v] to (pick random (-240) to (240)) set [PersonY v] to (pick random (-180) to (180)) hide forever switch to costume [Mouse pointer v] go to x: (mouse x) y: (mouse y) stamp switch to costume [Hiding person v] go to x: (PersonX) y: (PersonY) if <(touching [mouse pointer v]?) and (mouse down?)> then show say [You found me!] for (2) secs stop [this script v] else if <(timer) > (5)> then reset timer set [PersonX v] to (pick random (-240) to (240)) set [PersonY v] to (pick random (-180) to (180)) end end
- Showing how long a Scratcher is taking in a project (a variable can also be used for this, but it is not as accurate)
when I receive [Start the homework quiz! v]//the variable timer is showing reset timer say [Don't forget to look at the timer to see how much time you've taken up!]
- Checking if an amount of time is acceptable for advancing to another stage of the project
when I receive [new level v] reset timer when I receive [level completion v] if <(timer) < (30)> then say [You beat the level! Let's move onto the next one.] for (3) secs broadcast [new level v] else say [You didn't finish in 30 seconds. Maybe next time.] for (3) secs stop [all v] end
- Reacting to the length a button is held down by changing a variable faster:
when this sprite clicked //when the button is clicked reset timer change [points v] by (1) //first incrementation wait until <(timer) > (.4)> repeat until <<not <mouse down>> or <not <touching [mouse-pointer v]?>> //since the button has been held long change [points v] by (1) //add points faster wait (.1) secs //wait a small amount of time
Multiple timers
Multiple timers can be simulated with variables which record the time at the beginning of the session.
when gf clicked reset timer wait (1) secs set [timerA v] to (timer) wait (2) secs say (join [The old timer is:] (timer)) for (1) secs say (join [The new timer is:] ((timer) - (timerA))) for (1) secs