Revision 105aa9b5f1a2cd3ab4f2d4cbf9db2cd9a0c7bd39 authored by jgraham on 28 March 2018, 19:03:58 UTC, committed by GitHub on 28 March 2018, 19:03:58 UTC
1 parent 167a1d6
AddEventListenerOptions-once.html
<!DOCTYPE html>
<meta charset="utf-8">
<title>AddEventListenerOptions.once</title>
<link rel="author" title="Xidorn Quan" href="https://www.upsuper.org">
<link rel="help" href="https://dom.spec.whatwg.org/#dom-addeventlisteneroptions-once">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="log"></div>
<script>
test(function() {
var invoked_once = false;
var invoked_normal = false;
function handler_once() {
invoked_once = true;
}
function handler_normal() {
invoked_normal = true;
}
document.addEventListener('test', handler_once, {once: true});
document.addEventListener('test', handler_normal);
document.dispatchEvent(new Event('test'));
assert_equals(invoked_once, true, "Once handler should be invoked");
assert_equals(invoked_normal, true, "Normal handler should be invoked");
invoked_once = false;
invoked_normal = false;
document.dispatchEvent(new Event('test'));
assert_equals(invoked_once, false, "Once handler shouldn't be invoked again");
assert_equals(invoked_normal, true, "Normal handler should be invoked again");
document.removeEventListener('test', handler_normal);
}, "Once listener should be invoked only once");
test(function() {
var invoked_count = 0;
function handler() {
invoked_count++;
if (invoked_count == 1)
document.dispatchEvent(new Event('test'));
}
document.addEventListener('test', handler, {once: true});
document.dispatchEvent(new Event('test'));
assert_equals(invoked_count, 1, "Once handler should only be invoked once");
invoked_count = 0;
function handler2() {
invoked_count++;
if (invoked_count == 1)
document.addEventListener('test', handler2, {once: true});
if (invoked_count <= 2)
document.dispatchEvent(new Event('test'));
}
document.addEventListener('test', handler2, {once: true});
document.dispatchEvent(new Event('test'));
assert_equals(invoked_count, 2, "Once handler should only be invoked once after each adding");
}, "Once listener should be invoked only once even if the event is nested");
test(function() {
var invoked_count = 0;
function handler() {
invoked_count++;
}
document.addEventListener('test', handler, {once: true});
document.addEventListener('test', handler);
document.dispatchEvent(new Event('test'));
assert_equals(invoked_count, 1, "The handler should only be added once");
invoked_count = 0;
document.dispatchEvent(new Event('test'));
assert_equals(invoked_count, 0, "The handler was added as a once listener");
invoked_count = 0;
document.addEventListener('test', handler, {once: true});
document.removeEventListener('test', handler);
document.dispatchEvent(new Event('test'));
assert_equals(invoked_count, 0, "The handler should have been removed");
}, "Once listener should be added / removed like normal listeners");
</script>
![swh spinner](/static/img/swh-spinner.gif)
Computing file changes ...