-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
43 lines (42 loc) · 1.27 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>EventTarget</title>
<script src="EventTarget.js"></script>
</head>
<body>
<script>
const target=new EventTarget();
//example 1——you can add any function on the same event and transfer an object with.
target.addEvents("message",function (e) {
setTimeout(function () {
console.log(e.message);
},3000)
});
target.addEvents("message",function (e) {
setTimeout(function () {
console.log(e.message);
},3000)
});
target.trig({type:"message",message:"This is the thing in 3s to show."});
//example 2——remove an event
target.addEvents("fly",function () {
console.log("This was removed and could never be showed.");
});
target.removeEvents("fly");
target.trig({type: "fly"});
//example 3——remove a function in the event
const handler1=function () {
console.log("This was removed and could never be showed.");
};
const handler2=function(){
console.log("something from handler2.");
};
target.addEvents("close",handler1);
target.addEvents("close",handler2);
target.removeEvents("close",handler1);
target.trig({type: "close"});
</script>
</body>
</html>