concept threshold in category javascript
appears as: threshold, threshold

This is an excerpt from Manning's book JavaScript on Things: Hacking hardware for web developers.
The threshold value defaults to 1, which means that—because read sensor values are integers—any change in value will trigger the change event. threshold is an attribute on the Sensor object instance and can be changed at any point to alter the threshold for triggering change events. The following listing combines this into a script that logs out any time the sensor changes value by 5 or more (remember, possible values range from 0 to 1023).
Listing 9.13. Processing gesture data
APDS9960.prototype.processGesture = function () { const raw = this.gestureData.raw; const directionDelta = function (el1, el2, dir1, dir2) { 1 var el2r = ((el2[dir1] - el2[dir2]) * 100) / (el2[dir1] + el2[dir2]); var el1r = ((el1[dir1] - el1[dir2]) * 100) / (el1[dir1] + el1[dir2]); return el2r - el1r; }; const exceedsThreshold = raw.filter(sample => { 2 return (sample.up > GESTURE_THRESHOLD_OUT && sample.down > GESTURE_THRESHOLD_OUT && sample.left > GESTURE_THRESHOLD_OUT && sample.right > GESTURE_THRESHOLD_OUT); }); if (!exceedsThreshold.length || raw.length < 4) { 3 // If not enough data or none exceed threshold, nothing to do // This will result in gesture data being ignored and discarded return false; } const first = exceedsThreshold[0]; const last = exceedsThreshold[exceedsThreshold.length - 1]; const deltas = { upDown: directionDelta(first, last, 'up', 'down'), leftRight: directionDelta(first, last, 'left', 'right') }; this.gestureData.deltas = deltas; 4 };