0

I would like to create my own Event Handler. For good example, XMLHttpRequest, this has onreadystatechange method which can fetch the status events just like below coding.

I want to have similar function, but I could not find it out (I tried AddEventListener/Promise etc). Does anyone know how to implement this?

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){  
switch (xhr.readyState) {
    case 0:
        //not initialized
        break;
    case 1:
        //loading
        break;
    case 2:
        //waiting for response
        break;
    case 3:
        //receiving data
        break;
    case 4:
        if (xhr.status == 200){
            // Do something
        }
        break; 
    }
}
Winterhill
  • 33
  • 3
  • Does this answer your question? [Implementing events in my own object](https://stackoverflow.com/questions/10978311/implementing-events-in-my-own-object) – CS. Mar 09 '20 at 10:55
  • Thank you very much for your link. I will check and make sample code along with it. – Winterhill Mar 09 '20 at 15:21

0 Answers0