--- title: EventSource.close() slug: Web/API/EventSource/close translation_of: Web/API/EventSource/close original_slug: Server-sent_events/EventSource/close ---
{{domxref("EventSource")}} 的方法close()
用于关闭当前的连接,如果调用了此方法,则会将{{domxref("EventSource.readyState")}}这个属性值设置为 2 (closed)
Note: 如果连接已经被关闭,此方法不会做任何事情
eventSource.close();
None.
Void.
var button = document.querySelector('button'); var evtSource = new EventSource('sse.php'); button.onclick = function() { console.log('Connection closed'); evtSource.close(); }
Note: 你可以在Github上查看这整个例子: Simple SSE demo using PHP.
Specification | Status | Comment |
---|---|---|
{{SpecName('HTML WHATWG', "comms.html#dom-eventsource-close", "close()")}} | {{Spec2('HTML WHATWG')}} | Initial definition |
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
EventSource support | 6 | {{CompatNo}} | {{CompatGeckoDesktop("6.0")}} | {{CompatNo}} | {{CompatVersionUnknown}} | 5 |
Available in shared and dedicated workers[1] | {{CompatVersionUnknown}} | {{CompatNo}} | {{CompatGeckoDesktop("53.0")}} | {{CompatNo}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |
Feature | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
EventSource support | 4.4 | 45 | {{CompatNo}} | 12 | 4.1 |
Available in shared and dedicated workers[1] | {{CompatVersionUnknown}} | {{CompatGeckoMobile("53.0")}} | {{CompatNo}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |
[1] But not service workers as yet.