html5 - Can I make a Dart function "wait" for a set amount of time or input? -


i'm trying make simple rpg in dart. need show text on screen in div, , need make program wait user input before displaying next piece of text.

for example:

void main() {   showtext("hello, adventurer! welcome land of dartia! (press enter continue...)");   print("showtext has finished"); } 

"showtext has finished" should not display until text has been displayed , player presses enter. here's (pretty ugly in opinion) code have far:

void showtext(string text) {     var textbox = queryselector("#sample_text_id")         ..text = "";     var timer;     var out;     out = ([int = 0]) {         textbox.text += text[i];         if (i < text.length - 1) timer = new timer(const duration(milliseconds: 10), () => out(i + 1));     };     out(); } 

the timer runs out() function asynchronously, don't want do. ideally write this:

void showtext(string text) {     var textbox = queryselector("#sample_text_id")             ..text = "";     for(int = 0; < text.length; i++) {         textbox.text += text[i];         pause(const duration(milliseconds: 10)); //  pause program given duration     }     waitfor(keyenum.enter, keystateenum.down); // pause until key pressed (pseudo enum contains char codes) } 

is possible?

here's example of how using new async/await feature. note async declaration @ start of method bodies, , await statement before call pause() , showtext().

future pause(duration d) => new future.delayed(d);  future waitfor(int c) => document.body.onkeydown.firstwhere((e) => e.keycode == c);  future showtext(string text) async {     var textbox = queryselector("#sample_text_id")             ..text = "";     for(int = 0; < text.length; i++) {         textbox.text += text[i];         await pause(const duration(milliseconds: 100)); //  pause program given duration     }     return waitfor(keycode.enter); // pause until key pressed }  main() async {   await showtext("hello, adventurer! welcome land of dartia! (press enter continue...)");   print("showtext has finished"); } 

Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -