# VBForums CodeBank > CodeBank - JavaScript >  [JavaScript] Distinguish between WebSocket timeout and connection lost

## pimvdb

I made up this very trivial trick to determine whether a HTML5 WebSocket disconnected because the connection was lost or because no connection could be made.

I don't think it's such a complicated thing but if you need it, it might be helpful.

Anyway:



```
var ws = new WebSocket("...");
var connected = false;
ws.onopen = function() {
    connected = true;
}
ws.onclose = function() {
    if(!connected) {
        // it disconnected without being opened, i.e. timeout
    } else {
        // it disconnected whilst it had been opened before, i.e. connection lost
    }
    connected = false;
}
```

----------

