Ad space (banner)
๐ŸŽObjective-C Lessons
Lesson 56 / 68

Pausing for a set time ([NSThread sleepForTimeInterval:])

This lesson covers pausing in Objective-C, with the aim of understanding the basics of programs that deal with time.

[NSThread sleepForTimeInterval:seconds] pauses for the time you specify. Use it to space out calls to an API, or to slow work down deliberately.

The sample code prints "Starting", waits one second with [NSThread sleepForTimeInterval:1.0], and then prints "Ran after 1 second". Run it and you can feel the gap between the messages.

A common early stumble is that calling sleepForTimeInterval: on the main thread freezes the interface. In real app development, time-based work belongs on a background thread.

In professional work, this is applied to waiting briefly before retrying, so as not to hammer a server.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Objective-C directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)