2013-04-05 107 views
7

我想知道如何通過JavaScript關閉服務器發送的事件。下面是我的僞代碼:如何關閉服務器發送的事件事件

var ser=new EventSource("path"); 
ser.onmessage=function(ev){ 
if(!ev) 
    //want to close HERE!! 
else 
    console.log(ev); 
} 
+0

https://developer.mozilla.org/en-US/docs/Server-sent_events/EventSource#Method_overview - 的'.close( )'方法? – Ian 2013-04-05 16:58:57

+1

對不起,我一直在寫關閉()這一次..固定 – ahhmarr 2013-04-05 17:06:04

回答

5

這裏是我發現的方式工作最適合我

var eventSource = new EventSource("path"); 
eventSource.onerror = eventSourceErrorFunction; 
var eventSourceErrorFunction = function(event) 
{ 
     if (event.eventPhase == EventSource.CLOSED) { 
      that.eventSource.close(); 
      console.log("Event Source Closed"); 
     } 
} 
相關問題