Skip to content

TimeOut

Jordan Samhi edited this page Aug 22, 2023 · 1 revision

TimeOut ⏳

TimeOut is a class designed to provide a simple mechanism for setting a timeout duration, launching a timer, and triggering a task when the timeout expires.

Table of Contents

Overview

The TimeOut class is used to create a timer that can execute a predefined task when the specified timeout duration is reached. It is a part of the AndroSpecter framework.

Constructor

TimeOut(int n)

Constructs a new TimeOut object with a specified timeout duration. If zero, the timeout duration defaults to 60 seconds.

Methods

launch()

Launches the timer with the specified timeout duration.

cancel()

Cancels the currently running timer, if any.

getTimeout() -> int

Gets the timeout duration of this TimeOut object.

Examples

Example 1: Creating and Launching a Timer with a Custom Timeout

TimeOut timeOut = new TimeOut(30); // 30 seconds timeout
timeOut.launch();

Example 2: Creating a Timer with Default Timeout and Cancelling It

TimeOut timeOut = new TimeOut(0); // Defaults to 60 seconds
timeOut.launch();
// Some code or task here
timeOut.cancel(); // Cancels the timer before the timeout is reached

Example 3: Getting the Timeout Duration

TimeOut timeOut = new TimeOut(45);
int timeoutDuration = timeOut.getTimeout();
System.out.println("Timeout Duration: " + timeoutDuration + " seconds"); // Outputs: 45 seconds

These examples demonstrate how to use the TimeOut class to create and control a timer, either with a custom timeout duration or a default value. It offers an easy way to integrate time-sensitive operations within applications.