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.
let seconds = UInt64(2)
print("paused at \(Date())")
Task {
do {
try await Task.sleep(nanoseconds: seconds * 1_000_000_000)
print("started again at \(Date()) after \(seconds) seconds")
} catch {
print("An error occurred: \(error.localizedDescription)")
}
}
----------------------------------------------------------------
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)