How to create a custom event in javascript

2

Good, I am created a function that verifies the state of a global variable, this variable must be constantly verifying it to know if I change its value. The idea is that call this function and that it can return for example an event of type success() , para saber cuando termino la llamada o en este caso, cuando cambio de valor la variable .

function verificar() {
    var interval = setInterval(function () {
        if (oXMLHTTP.readyState == 4) {
            //CREAR NUEVO EVENTO
        }
    }, 1000);
}

verificar().success(){
    alert('Test');
}

Attentive to any suggestions.

    
asked by Yilo 10.04.2017 в 23:21
source

4 answers

0

You can take a callback to find out if your function ends.

for example something like that.

function verificar(callback) {
var interval = setInterval(function () {
    if (oXMLHTTP.readyState == 4) {
        //CREAR NUEVO EVENTO
        callback(true)
    }


   }, 1000);
}

    verificar(function(response){
      alert(response);
    })
    
answered by 11.04.2017 / 00:01
source
1

It is normal to do it through the event load of XMLHttpRequest :

request.onload = () => {
  if (request.readyState === 4 && request.status === 200) {
    // debes enviar como respuesta: 'CHANGED' si la variable cambió
    if (request.responseText === 'CHANGED') {
      // variable cambió
    }
  }
};

Making requests every second penalizes performance, keep that in mind. Instead, perform verification in processes where you can change the variable , this way you save bandwidth and memory.

    
answered by 11.04.2017 в 02:09
0

There are paradigms such as event-oriented programming or reactive programming. they create a flow of events in a particular way. using a bit of that concept we can make the changes of states detected. in general all frameworks have it.

I do not know what framework you use but this response show how to subscribe to the oXMLHTTP.onreadystatechange event

function ProcessSend() 
    Set oXMLHTTP = CreateObject("MSXML2.XMLHTTP.4.0")
    Set oXMLDoc = CreateObject("MSXML2.DOMDocument")

    oXMLHTTP.onreadystatechange = getRef("HandleStateChange") 

    strEnvelope = "callNo="&callNo&"&exp="&exp

    call oXMLHTTP.open("POST","http://localhost:11883/ServiceCall.asmx/"&posFirm,false)'<< changed true to false here.
    call oXMLHTTP.setRequestHeader("Content-Type","application/x-www-form-urlencoded")


    call oXMLHTTP.send(strEnvelope)
end function

Sub HandleStateChange 
    if(oXMLHTTP.readyState = 4) then
        dim szResponse: szResponse = oXMLHTTP.responseText
        call oXMLDoc.loadXML(szResponse)
        if(oXMLDoc.parseError.errorCode <> 0) then
                'call msgbox("ERROR")
                response = oXMLHTTP.responseText&" "&oXMLDoc.parseError.reason
                'call msgbox(oXMLDoc.parseError.reason)
        else
                response = oXMLDoc.getElementsByTagName("string")(0).childNodes(0).text
        end if

    end if
End Sub
    
answered by 11.04.2017 в 00:22
0

Apparently you try to use the wep API XMLHttpRequest if so, because you do not try the following:

oXMLHTTP.addEventListener("load", function (){
 alert('load successful!');
});
    
answered by 11.04.2017 в 00:16