1

I want to stop/sleep executing to simulate long time process, unfortunately I can't find information about it. I've read the following topic (How can I "sleep" a Dart program), but it isn't what I look for.

For example sleep() function from dart:io packages isn't applicable, because this package is not available in a browser.

For example:

import 'dart:html';
main() {
  // I want to "sleep"/hang executing during several seconds
  // and only then run the rest of function's body
  querySelect('#loading').remove();
  ...other functions and actions...
}

I know that there is Timer class to make callbacks after some time, but still it doesn't prevent the execution of program as a whole.

Community
  • 1
  • 1
Timur Fayzrakhmanov
  • 17,967
  • 20
  • 64
  • 95

2 Answers2

3

There is no way to stop execution. You can either use a Timer, Future.delayed, or just use an endless loop which only ends after certain time has passed.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
2

If you want a stop the world sleeping function, you could do it entirely yourself. I will mention that I don't recommend you do this, it's a very bad idea to stop the world, but if you really want it:

void sleep(Duration duration) {
  var ms = duration.inMilliseconds;
  var start = new DateTime.now().millisecondsSinceEpoch;
  while (true) {
    var current = new DateTime.now().millisecondsSinceEpoch;
    if (current - start >= ms) {
      break;
    }
  }
}

void main() {
  print("Begin.");
  sleep(new Duration(seconds: 2));
  print("End.");
}