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

  1. if (!isDone++) { 343,020,200 single coercion post fix

  2. if (isProceding && isProceding--) { 342,466,581 short circuit post fix

  3. if (isProceding) { isProceding = false; 338,360,292 standard

  4. if (0 < isProceding--) { 278,447,221 comparison coercion post fix

  5. 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
}