Search code examples
javascriptjqueryajaxphonegap

Javascript how to trigger ajax call after a function finishes


Iam developing an application using Phonegap, everything is going fine but I want an ajax to be triggered after i get the qr code value from another function at the same page, is there a way to trigger it right after theuser gets the scan value? the code is a bit messy...

    <script type="text/javascript">
            $(document).ready(function() {
 `var uemail = localStorage.getItem("semail"); 
document.getElementById("x").innerHTML = uemail;`

      var dataString = "email=" + email + "&secode=" + value + "&insert=";
            var h = localStorage.getItem("secode");
        $("#conf").click(function() {
                $.ajax({
                    type: "POST",
                    url: "http://example.com/auth.php?email="+uemail+"&secode="+h+"",
                    data: dataString,
                    crossDomain: true,
                    cache: false,
                    beforeSend: function() {
                     $('#loading').show();
                    },
                    success: function(data) {

                        if (data == "success") {

                             alert("DONNNEEE");
                        } else if (data == "error") {

                        alert("error");
                        }
                    }
                });
                return false;
                  });
                   });

      function logoutck() {
    var r = confirm("هل انت واثق من ذلك؟");
    if (r) {
       window.location.href = 'login.html'
    }
}

    function scan()
            {
                cordova.plugins.barcodeScanner.scan(
                    function (result) {
                        if(!result.cancelled)
                        {

                            if(result.format == "QR_CODE")
                            {
                             var value = result.text;
                            localStorage.setItem("secode", value);
                               var h = localStorage.getItem("secode");
                               alert(h);
                            }
                        } else{
                        alert("U CANCELLED IT");
                        }
                    },
                    function (error) {
                        alert("Scanning failed: " + error);
                    }
               );
               }


</script>

</head>

<body>
<div id="loading">

</div>
    <div class="bar bar-header bar-positive" style="margin-bottom:80px;">
        <h1 class="title">EasyQ</h1>
        <a class="button button-clear" onclick="logoutck();">خروج</a>
    </div>

    <br/>
    <br/>
    <div class="container">




    <p style="text-align: right;">:انت مسجل دخولك ك</p>

  <div align="right"><span class="badge" align="right" dir="rtl" style="text-align: right;" id="x"></span></div>
  <br>

 <a target="_blank" href="javascript:scan();" style="text-decoration: none"><button>Scan</button></a>
  <br><br><br><br>
  <button id="conf">confirmo</button>
  <script> 
  $(document).ready(function() {
  var uemail = localStorage.getItem("semail");
document.getElementById("x").innerHTML = uemail;

      var dataString = "email=" + email + "&secode=" + value + "&insert=";
            var h = localStorage.getItem("secode");
        $("#conf").click(function() {
                $.ajax({
                    type: "POST",
                    url: "http://easyq.hol.es/easyqappfiles/qrauth.php?email="+uemail+"&secode="+h+"",
                    data: dataString,
                    crossDomain: true,
                    cache: false,
                    beforeSend: function() {
                     $('#loading').show();
                    },
                    success: function(data) {

                        if (data == "success") {

                             alert("DONNNEEE");
                        } else if (data == "error") {

                        alert("error");
                        }
                    }
                });
                return false;
                  });
                   });


</script>

Solution

  • Write your ajax call as a function and call it when you need it.

    function ajaxCall(){
      $.ajax({
        success: function(){
            // do something
        }
      });
    }
    

    And then

    if(result.format == "QR_CODE") {
      ajaxCall();
    }
    

    You can also pass parameter to the function if needed