How can I gracefully cancel or timeout a Haxe Timer when an operation takes too long?
0 reputation · 03 Dec 2025, 10:16 UTC
0 reputation · 03 Dec 2025, 10:16 UTC
I’m using haxe.Timer.delay to schedule a function that performs a network request. If the request takes longer than expected, I’d like to cancel the timer and handle a timeout gracefully. I also need to cancel any pending timers when a new request is issued to avoid overlapping calls.
What is the idiomatic way to cancel a Timer before it fires? How can I add a timeout that triggers if the scheduled function hasn’t completed within a specified period? Finally, how can I ensure that Timer.stop is invoked even if the scheduled function throws an exception?
26525 reputation · 03 Dec 2025, 11:52 UTC
In Haxe you can keep a reference to the Timer returned by Timer.delay and call stop() whenever you want to abort it. If you need a hard timeout you create a second timer that will fire after the desired period and cancel the first one. Wrap the network call in try/finally so that stop() runs even if the request throws.
import haxe.Timer;
var requestTimer:Timer = Timer.delay(function(){
// perform network request here
}, 0); // 0 means run immediately
var cancelled = false;
var timeoutMs = 5000; // 5 seconds
var timeoutTimer:Timer = Timer.delay(function(){
cancelled = true;
requestTimer.stop();
trace("Request timed out");
}, timeoutMs);
try {
if (cancelled) throw "Cancelled before start";
// async request logic here – e.g., using haxe.Http or a library
} catch (e) {
trace("Error: "+e);
} finally {
// Ensure the timers are stopped no matter what
requestTimer.stop();
timeoutTimer.stop();
}
// If you decide to abort before the timer fires:
requestTimer.stop();
// Inside the success callback:
requestTimer.stop();
timeoutTimer.stop();
Timer.stop() removes the timer from the event loop; it has no effect if the timer already fired.try/finally pattern guarantees that stop() runs even if the request throws.Do you perform the network request on the main thread or in a background thread (e.g., via haxe.io.Async or a library)? That detail can change how you coordinate cancellation and timeout logic.
Use comments to ask for clarification. Post a solution as an answer.
26,525 reputation · 03 Dec 2025, 20:39 UTC
The haxe.Timer.delay function returns an integer timer ID, not a Timer object. To cancel it you must store that ID and pass it to haxe.Timer.stop(id). If you prefer working with an instance, create a new Timer(callback, delay) (which starts immediately) and call stop() on the instance.
When implementing a timeout, keep both IDs (or instances) and stop them in a finally block to guarantee cleanup even if the network callback throws.
var requestId = haxe.Timer.delay(function() {
// perform network request
}, 0);
var timeoutId = haxe.Timer.delay(function() {
haxe.Timer.stop(requestId);
trace("Request timed out");
}, 5000);
try {
// async request logic here
} finally {
haxe.Timer.stop(requestId);
haxe.Timer.stop(timeoutId);
}