Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a javascript function to run after another one get finished?

I'm using Apache Cordova to develop mobile application. the problem is that I want to get mobile phone number and then send it via jQuery get function for authorization. all functions are OK but the function that get mobile phone number is slower than other and it get finished last.

a summary of my code is this :

document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
    devicePhoneNumber();
    alert("ALERT1"); // ALERT1
};
function devicePhoneNumber() {
    var telephoneNumber = cordova.require("telephonenumber");
    telephoneNumber.get(function (result) {
    alert(result); //ALERT2
    }, function () {
        alert("error");
    });
};

I don't know why first a get ALERT1 and after that i get ALERT2. I want to run my other codes after I get ALERT2.

Any suggestion would be appreciated.

like image 476
Salman Avatar asked Sep 12 '14 06:09

Salman


People also ask

How do you make a function execute after another in JavaScript?

function a() { // first function code here $(document). trigger('function_a_complete'); } function b() { // second function code here } $(document). bind('function_a_complete', b); Using this method, function 'b' can only execute AFTER function 'a', as the trigger only exists when function a is finished executing.

How will you force these calls to execute one after the other?

Perform a step in the animation. Call setTimeout with a function containing the next animation step and a delay. Some time passes. The callback given to setTimeout executes.

How do you delay a function in JavaScript?

To delay a function call, use setTimeout() function. functionname − The function name for the function to be executed. milliseconds − The number of milliseconds. arg1, arg2, arg3 − These are the arguments passed to the function.

Does JavaScript execute sequentially?

39.1. 2 JavaScript executes tasks sequentially in a single process. This loop is also called the event loop because events, such as clicking a mouse, add tasks to the queue.


2 Answers

If telephone.get is async, you need to wait for it to finish before you can do your first alert

document.addEventListener("deviceready", onDeviceReady, false);

Write your devicePhoneNumber function to accept a callback done. The callback receives two parameters, err (if present) and result. Regardless of the telephoneNumber.get, the callback will still be called

function devicePhoneNumber(done) {
  var telephoneNumber = cordova.require("telephonenumber");
  telephoneNumber.get(function (result) {
    done(null, result);
  }, function () {
    done(Error("There was an error getting the phone number."));
  });
}

To use the function now, pass function a callback that accepts the two parameters err and result. In your callback, check for the error. If it is present, handle it accordingly. You can access the error's message with err.message.

function onDeviceReady() {
  devicePhoneNumber(function(err, result) {
    if (err) return alert(err.message);
    alert("Alert 1");   // alert 1
    alert(result);      // alert 2
  });
}
like image 175
Mulan Avatar answered Oct 18 '22 09:10

Mulan


add a callback function to your devicePhoneNumber() function:

document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
    devicePhoneNumber(function(){ //anonymous function for the callback parameter
        /* everything you put in here will be executed only AFTER 
        telephoneNumber.get() has run successfully */
        alert("ALERT1"); // ALERT1
    });

};
function devicePhoneNumber(callback) {
    var telephoneNumber = cordova.require("telephonenumber");
    telephoneNumber.get(function (result) {
    alert(result); //ALERT2
    callback(); //callback function is called here
    }, function () {
        alert("error");
    });
};
like image 43
low_rents Avatar answered Oct 18 '22 10:10

low_rents