Checking internet connection using XMLHttpRequest()

With reference to the link Check internet connection i tried to implement the same in my code. I added the code in the file which will be first loaded in the project I have two queries

  1. When i keep the internet connection on then also im getting Connection does’nt exists, im pointing to
  2. Suppose my connection is lost mid and there is no activity going on on the application. No click no saving of the data, then also the readyStateChange event will fire? Or will i have to keep it calling on certain time interval?