Call a Function After Previous Function Is Complete

Call a function after previous function is complete

Specify an anonymous callback, and make function1 accept it:

$('a.button').click(function(){
if (condition == 'true'){
function1(someVariable, function() {
function2(someOtherVariable);
});
}
else {
doThis(someVariable);
}
});

function function1(param, callback) {
...do stuff
callback();
}

How to call functions after previous function is completed in python?

Use a global variable. Set it in function1, and check it in function2.

function1_success = False

def function1():
global function1_success
# do stuff ...
if success:
function1_success = True

def function2():
global function1_success
if function1_success:
# do stuff ...

Javascript call function after another function executes

Well since it is an asynchronous task, you need to add logic to tell the next code when to run. Classic way is to use a a callback method:

function make_base(img, callback) {
base_image = new Image();
base_image.src = img;
base_image.onload = function(){
context.drawImage(base_image, 0, 0);
callback()
}
}

function text(text) {
context.fillText(text, 50, 50)
}

function render() {
make_base(xxx, function () {
text(xxx)
})
}

or move into modern patterns is to use a promise

function make_base(img) {
return new Promise(function(resolve, reject) {
base_image = new Image();
base_image.src = img;
base_image.onload = function(){
context.drawImage(base_image, 0, 0);
resolve()
}
}

function text(text) {
context.fillText(text, 50, 50)
}

function render() {
make_base(xxx).then(function () {
text(xxx)
})
}

How to call functions after previous function is completed in Flutter?

Transform your popUpForGroup to async function like this.

Future<void> popUpForGroup({...}) {...}

After that and inside your onPressed Function you can write this:

onPressed: () async => {
await popUpForGroup("Add Group", null, context),
print("pop up closed"),
refreshPage(),
},

Execute javascript function after previous one finishes

There's many different approaches to this but I would suggest using a Promise like this:

document.getElementById('btnSend').addEventListener('click', e => {    e.preventDefault();    var result = validateInputs(email, subject, message);    if(result){        showElement(document.getElementById('formGroupSpinner'), 2000).then(()=>{            return showElement(document.getElementById('formGroupSuccessImg'), 2000);        }).then(()=>{            resetForm();        });    }});
//expects an html element and a number representing miliseconds. Shows the html element for that amount of time.function showElement(element, mSeconds) { return new Promise((resolve, reject) => { element.classList.remove('d-none'); element.classList.add('d-block'); setTimeout( () => { element.classList.remove('d-block'); element.classList.add('d-none'); resolve(); }, mSeconds); });}


Related Topics



Leave a reply



Submit