Question

I'm trying to add to this code so it will auto-run the string for button.g == 1, after 10 seconds of not clicking the button.

  {
    if(button.g == 0)
    {
        f.a(parent);
        AutoJoin.instance.resetCache();
    }
    if(button.g == 1)
    {
        AutoJoin.instance.screen = new AutoJoinScreen(parent, info);
        f.a(AutoJoin.instance.screen);
    }
    if(button.g == 2)
        f.a(new PropertiesScreen(this, info.ip));
}
Was it helpful?

Solution

You can use setTimeout to "delay" an action:

setTimeout(function() { console.log('I'm 10 secs later'); }, 1000*10);

With clearTimeout you can abort such a timer:

var timer = setTimeout(/* .... */);
clearTimeout(timer)

In your example you could start a timeout which executes the logic for button.g == 1 and cancel the timer if any button is clicked:

Setting the timer to 10s:

var timer = setTimeout(1000*10, function() { 
  AutoJoin.instance.screen = new AutoJoinScreen(parent, info);
    f.a(AutoJoin.instance.screen);
 })

Click routine:

clearTimeout(timer);

if(button.g == 0) {
    f.a(parent);
    AutoJoin.instance.resetCache();
} 
else if if(button.g == 1) {
    AutoJoin.instance.screen = new AutoJoinScreen(parent, info);
    f.a(AutoJoin.instance.screen);
}
else if(button.g == 2) {
    f.a(new PropertiesScreen(this, info.ip));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top