There's a bit of a delay caused by executing the JavaScript itself (it's not that fast). If you instead do:
function checkTime()
{
var t=getTime();
if (startTime==0) startTime=t-1;
...
function testTime()
{
startTime=0;
...
So you're comparing subsequent setIntervals with the time from the first, you should get a much better accuracy.
Espruino isn't pre-emptive, but it tries to schedule intervals so that on average they are very accurate. If you add a watch crystal to Espruino you can happily run it as a reasonably accurate clock using nothing more than setInterval(...,1000).
So for instance something like this could happen:
checkTime called at time 3.3 (0 sec)
wasteTime called at time 4.2, takes 300ms
checkTime called at time 4.5 (1.2 sec)
checkTime called at time 5.3 (2 sec) <-----
Hope that helps. It's worth noting that by default Espruino uses the internal RC oscillator for time, which could be +/- 2%. For proper logging you'll want to add a watch crystal - see http://www.espruino.com/Clocks
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
There's a bit of a delay caused by executing the JavaScript itself (it's not that fast). If you instead do:
So you're comparing subsequent setIntervals with the time from the first, you should get a much better accuracy.
Espruino isn't pre-emptive, but it tries to schedule intervals so that on average they are very accurate. If you add a watch crystal to Espruino you can happily run it as a reasonably accurate clock using nothing more than
setInterval(...,1000)
.So for instance something like this could happen:
Hope that helps. It's worth noting that by default Espruino uses the internal RC oscillator for time, which could be +/- 2%. For proper logging you'll want to add a watch crystal - see http://www.espruino.com/Clocks