Q(uick)BASIC Statement: SLEEP

Quick View

SLEEP

A control flow statement that suspends execution of the calling program

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • SLEEP [seconds&]
Description/Parameter(s)
seconds& Number of seconds to suspend the program.
If seconds& is 0 or is omitted, the program is suspended until a key is pressed or a trapped event occurs.
Example
PRINT "Taking a 10-second nap..." SLEEP 10 PRINT "Wake up!"

See also:

Syntax
  • SLEEP [seconds]
Description/Parameter(s)

The optional argument seconds determines the number of seconds the program is suspended.

The SLEEP statement suspends the program until one of the following events occurs:

  • The time period in the seconds argument has elapsed.
  • A key is pressed.
  • An enabled event occurs.

If seconds is zero, or not specified, the program is suspended indefinitely. Only a keystroke or an enabled event can interrupt an indefinite suspension.

SLEEP responds only to keystrokes that occur after it executes. SLEEP ignores characters in the keyboard buffer that were typed before it executed.

An event (such as ON COM or ON TIMER) cannot interrupt a SLEEP suspension unless its trapping is active when the event occurs. This means that trapping must have been initialized with an ON event statement, turned on with an event ON statement, and not have been disabled with an event OFF statement or an event STOP statement.

Example

The following program suspends execution for 10 seconds. There is no ON event statement, so the only way to interrupt the suspension before 10 seconds have passed is to press a key.

CLS 'Clear the screen PRINT "Taking a 10 second nap..." SLEEP 10 PRINT "Awake!" END

See also:

Syntax
  • SLEEP (seconds&)
Description/Parameter(s)
seconds& Number of seconds to suspend the program.
If seconds& is 0 or is omitted, the program is suspended until a key is pressed or an enabled event occurs.

Usage Notes

  • SLEEP responds only to keystrokes that occur after it executes.
  • SLEEP ignores characters in the keyboard buffer that were typed before it executed.
  • An event (such as ON COM or ON TIMER) cannot interrupt a SLEEP suspension unless its trapping is active when the event occurs. This means that trapping must have been initialized with an ON event statement, turned on with an event ON statement, and not have been disabled with an event OFF statement or an event STOP statement.
Example

This example uses the SLEEP statement to suspend execution for 10 seconds. There is no ON event statement, so the only way to interrupt the suspension before 10 seconds have passed is to press a key.

PRINT "Taking a 10 second nap..." SLEEP 10 PRINT "Awake!" END

See also: