-
• #2
If you have the function handy you can do
Puck.removeListener('mag', onMag)
: http://www.espruino.com/Reference#l_Object_removeListenerNote that if you do:
function main() { function onMag(m) { m.x -= zero.x; m.y -= zero.y; m.z -= zero.z; var v=Math.sqrt(m.x*m.x+m.y*m.y+m.z*m.z).toFÂixed(0); console.log(v); } Puck.removeListener('mag', onMag); Puck.on('mag', onMag); Puck.magOn(); }
It won't work though, because
onMag
is basically a different function each time.Or you can just do
Puck.removeAllListeners('mag')
: http://www.espruino.com/Reference#l_Object_removeAllListenersBut personally if you can I'd just make sure you only call
Puck.on('mag',
once - there's no need to call it at startup each time because Espruino will remember.
Hi @Gordon,
Suppose you have
Multiple calls to main() will result multiple calls to onMag() function, which once the mag is stopped (Puck.magOff()) all are stopped and started if the mag is on (Puck.magOn()). Is there something like an id of the previous callback in order to be stopped ?
Calling main() 10 times you will have v like that:
21
1167
2355
3543
4731
5919
7107
8295
9484
10672
i.e. mag values are exploding even without magnet close to the Puck.
I came upon this issue because in general I have:
which once the code is uploaded everything is working fine, but when saved I have two callbacks on the mag, if you save it again you will have three callbacks on the mag, etc. How can I have a single function where to receive the current mag values ?
Thank you!