0

see I have code like this

          function validate(){
            if (document.getElementById('<%=txtSeqNo.ClientId %>').value.trim() == "") {
                alert('Please enter Seuenceqnumer.');
                return false;
            }
            var result = checkduplicateseq();
            if (result) {
                return true;
            }
            else {
                return false;
            }           
        }

and definitation for checkduplicateseq is

function checkduplicateseq() {
            var result = true;
            if ($('[id*=ctl00_CPHMainPageLambda_chkoperationlist] input[type="checkbox"]:checked').length > 0) {
                var seqNo = $("#ctl00_CPHMainPageLambda_txtSeqNo").val();
                var chkvalue = $('[id*=ctl00_CPHMainPageLambda_chkoperationlist] input[type="checkbox"]:checked').parent().parent().find("span").attr("datavalue");
                var hfmode = $("#ctl00_CPHMainPageLambda_hd_SequenceNo").val();
                var oldoperationid = $("#ctl00_CPHMainPageLambda_hd_operationo").val();
                if (seqNo == "") {
                }
                else {
                    $.ajax({
                        type: "POST",
                        url: "frmFAQMst.aspx/GetSequenceNoforOperation",
                        contentType: 'application/json; charset=utf-8',
                        dataType: 'json',
                        data: '{"OptionaId":"' + chkvalue + '","oldoperationid":"' + oldoperationid + '","seqNo":"' + seqNo + '","hfmode":"' + hfmode + '"}',
                        error: function (ex) {
                            console.log(ex);
                        },
                        success: function (response) {
                            if (response.d == "1") {

                                alert("Sequence Number already exist!");
                                $("#ctl00_CPHMainPageLambda_txtSeqNo").attr('value', '')
                                $("#ctl00_CPHMainPageLambda_txtSeqNo").focus();
                                result = false;
                            }
                            else {
                                result = true;
                            }
                        }
                    });
                }
            }
                return result;
        }

now if i call checkduplicateseq from validation function at the last and store return value of checkduplicateseq fucntion in variable like

var result = checkduplicateseq();

in browser i can see the value of result = undefine

so it goes to else part of that function

    if (result) {
        return true;
        }
   else {
           return false;
        }

and in it return false so further execution not work i want to go further after finishing checkduplicateseq (ajax call)

Tarang
  • 66
  • 3
  • 13

3 Answers3

1

use a callback in your success function. you can pass the callback into your checkduplicateseq function, or just leave it in the global namespace and call it directly from your success function, or you can just inline that function altogether (defined inside success function)


checkduplicateseq(someCallback);

function someCallback () {
    return !!result
}

and your success function

success: function(response) {
    if (response.d == "1") {

        alert("Sequence Number already exist!");
        $("#ctl00_CPHMainPageLambda_txtSeqNo").attr('value', '')
        $("#ctl00_CPHMainPageLambda_txtSeqNo").focus();
        result = false;
    } else {
        result = true;
    }

    someCallback();
}

The best way, if you're chaining lots of callbacks, is to research Promises, which allow you to write in a sequential fashion, rather than passing things to be invoked later

neaumusic
  • 10,027
  • 9
  • 55
  • 83
  • its not working !! Or may be I am doing some wrong .. please elaborate .. – Tarang Apr 01 '16 at 08:44
  • you can define sections of code to run later, and pass them around. "callback" is a term for one of these named functions being passed in, with the understanding that it won't be invoked until a later time. in your example, success is the async function that runs once data is received. error would also be a function that runs when data is received. both of them can kick off the next function (callback) – neaumusic Apr 02 '16 at 01:30
  • a common practice is something like `window.addEventListener("click", log);` where you have a `function log (e) {console.log("hello world", e);}` and the reason you name it is so that you can remove the listener later with `window.removeEventListener("click", log);`, whereas if you had defined the listener `window.addEventListener("click", function (e) {console.log("hello world", e);});` you would lose reference to that anonymous function and cant specifically remove it. most listeners pass an `event` object as the first argument, which people call `e` – neaumusic Apr 02 '16 at 01:32
0

You can add the option of async: false to the $.ajax() function which will cause the process to wait. This is generally considered bad practice though.

e.g.

$.ajax({
  async: false,
  type: "POST",
  url: "frmFAQMst.aspx/GetSequenceNoforOperation",
  contentType: 'application/json; charset=utf-8',
  dataType: 'json',
.... etc
Drummad
  • 722
  • 1
  • 6
  • 23
0

I was checking on this issue i just found a similar question in stack overflow. Please check this link

How to do sequential asynchronous ajax requests with given number of streams

Community
  • 1
  • 1
suyesh
  • 659
  • 1
  • 10
  • 19