J'aime la fonction between
Pointy, j'en ai donc écrit une similaire qui a bien fonctionné pour mon scénario.
/**
* Checks if an integer is within ±x another integer.
* @param {int} op - The integer in question
* @param {int} target - The integer to compare to
* @param {int} range - the range ±
*/
function nearInt(op, target, range) {
return op < target + range && op > target - range;
}
donc si vous vouliez voir si x
était à ±10 de y
:
var x = 100;
var y = 115;
nearInt(x,y,10) = false
Je l'utilise pour détecter un appui long sur mobile :
//make sure they haven't moved too much during long press.
if (!nearInt(Last.x,Start.x,5) || !nearInt(Last.y, Start.y,5)) clearTimeout(t);