Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, in my page I have a textbox. While user enter a text a event is firing.

document.body.addEventListener('change', function ( event ) {
if (event.srcElement.className == 'dynChange') {
debugger;
serailObject = {};
var keyString = event.srcElement.value;
var txtId = event.srcElement.id;
var isExist = isSerialExist(keyString);

debugger;
if(!isExist){
var result = licenseArray.filter(obj => {
return obj.ID == itemId && obj.txtID == txtId
})
if(result.length==0){ 
serailObject.ID = itemId;
serailObject.license = keyString;
serailObject.txtID =txtId;
licenseArray.push(serailObject);

}
else{
for (var i = 0; i < licenseArray.length; i++) {
if(licenseArray[i].ID == itemId && licenseArray[i].txtID == txtId){
licenseArray[i].license = keyString;
}
}
} 
// assign this value in hidden field..

hdn_license.value = null;
hdn_license.value = JSON.stringify(licenseArray);
}
else{
alert('serial already exist');
event.srcElement.value = "";
}
}
});



As you could see I am calling a function
isSerialExist()


function isSerialExist(serial){
        debugger
        var returnVal = false;
        var result = licenseArray.filter(obj => {
            return obj.license == serial
        })
        if(result.length==0){
            var IStockTransSvc = new WcfAjaxServices.IStockTransaction();
            IStockTransSvc.IsSerialExist(serial, function (result, context, OnSuccess) {

               returnVal = result;
            }, function (error, context, OnError) {
                toastify("error", error, "System Error", "toast-bottom-right", true);
            }, null);
        }
        else{
            return returnVal;
        }
    }



Note:- licenseArray : this array contains all texts entered by the user.

This function checks whether there is already a same string inside  licenseArray if yes then return false and if not then it call a wcf service to check if that string is presented in database.But my code flows like this

Code flows..

1.Calling a function
2.Calling a service if string is not present inside licenseArray..
3.Before assigning the result into returnVal, it jumps into block 3.
4.Then it is assigning   the result into returnVal.

What I want:

I want block 4 must be executed before block 3. So that I could check this condition,

if(!isExist){ // isExist undefined now..
//.....
}


What I have tried:

I have tried what is mentioned in my questions..
Posted
Comments
Richard MacCutchan 3-Jan-19 5:31am    
Do not add your event listener until you are ready to handle it.
_jaws_ 3-Jan-19 7:51am    
Then what I need to do sir?
Richard MacCutchan 3-Jan-19 8:10am    
What I told you already. If your event handler is firing too early then you need to make sure you do not add it until you are ready to handle the events.
_jaws_ 3-Jan-19 8:45am    
No sir, its not the problem of event handling..Please have a look at the image :)

https://i.ibb.co/nrGSs59/Untitled.png
Richard MacCutchan 3-Jan-19 9:07am    
Sorry, but I am struggling to follow that code. I can only suggest that you use your debugger to follow the actual path as it executes.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900