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.
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.
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.
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.
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.
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
});
}
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");
});
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With