
- iOS Tutorial
- iOS - Home
- iOS - Getting Started
- iOS - Environment Setup
- iOS - Objective-C Basics
- iOS - First iPhone Application
- iOS - Actions and Outlets
- iOS - Delegates
- iOS - UI Elements
- iOS - Accelerometer
- iOS - Universal Applications
- iOS - Camera Management
- iOS - Location Handling
- iOS - SQLite Database
- iOS - Sending Email
- iOS - Audio & Video
- iOS - File Handling
- iOS - Accessing Maps
- iOS - In-App Purchase
- iOS - iAd Integration
- iOS - GameKit
- iOS - Storyboards
- iOS - Auto Layouts
- iOS - Twitter & Facebook
- iOS - Memory Management
- iOS - Application Debugging
- iOS Useful Resources
- iOS - Quick Guide
- iOS - Useful Resources
- iOS - Discussion
How to execute a task repeatedly after fixed time intervals in iOS
Apple has predefined class Timer, that fires after a certain time interval has elapsed, sending a specified message to a target object.
To read more about the Timer class you can check official apple documentation here
https://developer.apple.com/documentation/foundation/timer
To execute the task repeatedly after fixed interval of time we are going to use timer class. We are going to develop a sample application where the application prints hello Tutorials Point after every 5 seconds.
So let’s get started,
Step 1 − Open Xcode → New Project → Single View Application → Let’s name it “HelloTutotrialsPoint”
Step 2 − Open ViewController.swift and write one method doSomething() below ViewDidLoad(). Copy paste below code in your doSomething method().
private func doSomething() { let timer = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(ViewController.hello), userInfo: nil, repeats: true) }
Step 3: Implement/Create hello (selector ) as show below, and call doSomething() within ViewDidLoad().
@objc func hello() { print("hello") }
Your final code should look like below
import UIKit class ViewController: UIViewController, UITextFieldDelegate { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. self.doSomething() } private func doSomething() { let timer = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(ViewController.hello), userInfo: nil, repeats: true) } @objc func hello() { print("hello") } }
We’re done run your application and check out the output in console, you’ll see “hello” printing after time interval of 5 seconds.
- Related Articles
- How to execute Async task repeatedly after fixed time intervals in Android?
- How to execute an Async task repeatedly after fixed time intervals in Android using Kotlin?
- How to repeat a task repeatedly after a regular interval in Swift(iOS)?
- Schedule a task for repeated fixed delay execution in Java
- How to call a method after a delay in Swift(iOS)?
- How to Automatic Refresh a web page in a fixed time?
- Maximum sum after repeatedly dividing N by a divisor in C++
- Schedule a task for execution in Java after a given delay
- How to create a Scheduled task with a task scheduler using PowerShell?
- How to Create a Cron Job and Execute at a Given Time in Linux
- How to plot a time series array, with confidence intervals displayed in Python? (Matplotlib)
- How to get current date and time from internet in iOS?
- Execute a script after the document is printed in HTML?
- How to hide a widget after some time in Python Tkinter?
- How to make a setInterval() stop after some time or after a number of actions in JavaScript?
