I find that Simulator hangs after running makeJsonRequest recursively in the timer callback as shown in the following proof-of-concept code. The code is modified from the sample "Timer". I want to run makeJsonRequest every 10 sec to get data from a website. Is this the problem of the Simulator only? Since my device is not delivered yet, I wonder if the problem also exists on a real device. Any advice would be appreciated.
using Toybox.WatchUi as Ui;
using Toybox.Graphics as Gfx;
using Toybox.Timer as Timer;
using Toybox.Communications as Comm;
using Toybox.System as Sys;
var timer1;
class Dat
{
var dat1;
var dat2;
var dat3;
}
class MyWatchView extends Ui.View
{
var dat = new Dat();
function callback1()
{
Comm.makeJsonRequest("json-time.appspot.com/time.json",
{}, {}, method(:onReceive));
}
function onReceive(responseCode, data)
{
if( responseCode == 200 )
{
dat.dat1 = data["hour"];
dat.dat2 = data["minute"];
dat.dat3 = data["second"];
Sys.println(dat.dat1+":"+dat.dat2+":"+dat.dat3);
}
else
{
Sys.println( "Failed to load\nError: " + responseCode.toString() );
}
Ui.requestUpdate();
}
function onLayout(dc)
{
Comm.makeJsonRequest("json-time.appspot.com/time.json",{}, {}, method(:onReceive));
timer1 = new Timer.Timer();
timer1.start( method(:callback1), 10000, true );
}
function onUpdate(dc)
{
var string;
dc.setColor( Gfx.COLOR_BLACK, Gfx.COLOR_BLACK );
dc.clear();
dc.setColor( Gfx.COLOR_WHITE, Gfx.COLOR_TRANSPARENT );
string = "Time=" + dat.dat1 + ":" + dat.dat2 + ":" + dat.dat3;
dc.drawText( 40, (dc.getHeight() / 2) - 30, Gfx.FONT_MEDIUM, string, Gfx.TEXT_JUSTIFY_LEFT );
}
}
class InputDelegate extends Ui.BehaviorDelegate
{
function onMenu()
{
timer1.stop();
}
}
Best regards,
Anat