Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I do the equivalent of setTimeout + clearTimeout in Dart?

I'm porting some JavaScript to Dart. I have code that uses window.setTimeout to run a callback after a period of time. In some situations, that callback gets canceled via window.clearTimeout.

What is the equivalent of this in Dart? I can use new Future.delayed to replace setTimeout, but I can't see a way to cancel this. Nor can I find away to call clearTimeout from Dart.

like image 404
Danny Tuppeny Avatar asked Nov 21 '14 14:11

Danny Tuppeny


People also ask

How do I run setTimeout multiple times?

If you need to run a function multiple times, use the setInterval() method. To stop the timeout and prevent the function from executing, use the clearTimeout() method. The JavaScript setTimeout() method returns an ID which can be used in clearTimeout() method.

What does clearTimeout return?

clearTimeout(timeout) Clears an existing timer by passing in the numeric value returned by setTimeout(). Sometimes you need to clear a timeout timer before it executes. clearTimeout() uses the value returned by the setTimeout(function, milliseconds) function.

What does clearTimeout function do?

The global clearTimeout() method cancels a timeout previously established by calling setTimeout() .


1 Answers

You can use the Timer class

import 'dart:async';  var timer = Timer(Duration(seconds: 1), () => print('done'));  timer.cancel(); 
like image 196
Günter Zöchbauer Avatar answered Sep 22 '22 13:09

Günter Zöchbauer