locked
Need to fix this issue.. RRS feed

  • Question

  • User-2038777776 posted

    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

    https://i.ibb.co/nrGSs59/Untitled.png

    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..
    //.....
    }
    Thursday, January 3, 2019 8:02 AM

All replies

  • User-1174608757 posted

    Hi itz_babai

    According to your description, since  isSerialExist( ) is an  async function. The block 3 will be done before 'isExist' get the return value fromisSerialExist( ) block 4,so 'isExist' is always null and !isExist is always true.

    To solve this problem, I suggest you to  call function in block 3  in block 4. You could send a parameter  to block 4, like Ajax,after  block 4 check the value  parameter ,it will call function in block 3.

    I have made an ajax demo here to show the progress. I hope it could help you.

    aspx:

    <head runat="server">
        <title></title>
        <script src="../Scripts/jquery-3.3.1.js"></script>
        <script>
            $(function () {
                $.ajax({
                    type: "get",
                    url: "Data.ashx",
                    data: "data=check",
                    success: function (msg)
                    {
                        alert(msg);
                        
                    }
                })
    
            })
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
            </div>
        </form>
    </body>
    </html>

    data.ashx:

      public class Data : IHttpHandler
        {
    
            public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "text/plain";
                // context.Response.Write("Hello World");
                string s = context.Request.QueryString["data"];//get the parameter from aspx
                if (s == "check")
                {
                  context.Response.Write(  Say()); //call the function Say()
    
                }
            }
    
            public static string Say()
            {
    
                return "Check the data";
    
            }
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
    }

    Best Regards 

    Wei Zhang

    Friday, January 4, 2019 9:35 AM