JavaScript 后缀一个用于限制事件的布尔值/一次性标志
JavaScript postfix a boolean / one-shot flag for throttling an event
我想使用一次标志,然后无论如何将其转换为 false,在表达式 returns 之后的真实性:
var isProceding = true;
someObject.addEventListener("someEvent", doOnce); // event fires many times
function doOnce () {
if (isProceding) {
isProceding = false; // i want to join this line with the previous
// do stuff
someObject.removeEventListener("someEvent", doOnce);
}
}
from jsperf
if (!isDone++) {
343,020,200 single coercion post fix
if (isProceding && isProceding--) {
342,466,581 short circuit post fix
if (isProceding) { isProceding = false;
338,360,292 standard
if (0 < isProceding--) {
278,447,221 comparison coercion post fix
if (isProceding --> false) {
9,983,236 double coercion postfix
因为数字仅在 0 处为假
var isDone = 0;
if (!isDone++) {
// do stuff
}
或
var isProceding = true;
if (isProceding --> false) {
// do stuff
}
我想使用一次标志,然后无论如何将其转换为 false,在表达式 returns 之后的真实性:
var isProceding = true;
someObject.addEventListener("someEvent", doOnce); // event fires many times
function doOnce () {
if (isProceding) {
isProceding = false; // i want to join this line with the previous
// do stuff
someObject.removeEventListener("someEvent", doOnce);
}
}
from jsperf
if (!isDone++) {
343,020,200 single coercion post fix
if (isProceding && isProceding--) {
342,466,581 short circuit post fix
if (isProceding) { isProceding = false;
338,360,292 standard
if (0 < isProceding--) {
278,447,221 comparison coercion post fix
if (isProceding --> false) {
9,983,236 double coercion postfix
因为数字仅在 0 处为假
var isDone = 0;
if (!isDone++) {
// do stuff
}
或
var isProceding = true;
if (isProceding --> false) {
// do stuff
}