Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sleep for few milliseconds in swift 2.2?

Tags:

sleep

uikit

swift

please anyone tell me how to use sleep() for few milliseconds in swift 2.2?

while (true){     print("sleep for 0.002 seconds.")     sleep(0.002) // not working } 

but

while (true){     print("sleep for 2 seconds.")     sleep(2) // working } 

it is working.

like image 878
sulabh qg Avatar asked Jun 30 '16 09:06

sulabh qg


People also ask

How do you make a sleep in Swift?

You need to call Task. sleep() using await as it will cause the task to be suspended, and you also need to use try because sleep() will throw an error if the task is cancelled. Important: Calling Task. sleep() will make the current task sleep for at least the amount of time you ask, not exactly the time you ask.


1 Answers

usleep() takes millionths of a second

usleep(1000000) //will sleep for 1 second usleep(2000) //will sleep for .002 seconds 

OR

 let ms = 1000  usleep(useconds_t(2 * ms)) //will sleep for 2 milliseconds (.002 seconds) 

OR

let second: Double = 1000000 usleep(useconds_t(0.002 * second)) //will sleep for 2 milliseconds (.002 seconds) 
like image 142
Elijah Avatar answered Nov 13 '22 16:11

Elijah