execute code with 5 second delay in swift

main.swift
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
    // code to be executed after 5 seconds
}
99 chars
4 lines

The DispatchQueue.main.asyncAfter() method can be used to schedule a block of code to execute after a certain delay. The deadline parameter is a DispatchTime value representing the time at which the block should run, calculated as now plus the desired delay (TimeInterval). In this case, the code block will be executed on the main thread after a delay of 5 seconds.

gistlibby LogSnag