Javascript Function with Callback and Parameters

SBB

I am running into an issue with some basic JS functions when trying to add in a callback that needs to run another function with params.

Here is my email function:

function sendEmail(template, to, cc, bcc, callback, optional=null){

// Define vars needed
var body = '',
    subject = '';

// Based on the template..
switch(template){

    case 'privateNote':

        // Define the subject
        subject = 'Tool Request - Private Note Added';

        // Define our body
        body += 'Hello, <br /><br />';
        body += 'A new private note has been added to Request #' + requestID + '.<br/><br/>';
        body += 'To visit the request, click the following link: <a href="' + window.location.protocol + "//" + window.location.host + "/tool/Request2.php?id=" + requestID + '">' + window.location.protocol + "//" + window.location.host + "/tool/Request2.php?id=" + requestID + '</a>.';
        body += '<br /><br />';
        body += '<em>Message created by ' + userFirst + ' ' + userLast + '</em>';

}

// Send our email
$.ajax({
    url: "../resources/Classes/class.email.php",
    type: "POST",
    cache: false,
    data: {
        from: "[email protected]",
        to: to,
        cc: cc,
        bcc: bcc,
        subject: subject,
        body: body
    },
    error: function(err) {
        alert(err.statusText);
    },
    success: function(data) {
        // Handle Callback
        callFunction(callback);
    }
});
}

// Callbacks
function callFunction(func) {
    func();
}

// Reload the page
function refresh(){
    location.reload('true');
}

This is how I use the function:

sendEmail('privateNote', toArray, '', '', refresh, obj);

This is all working fine as expected, however I am faced with an issue.

There is a section where I need to send out two emails at the same time, one to the people who are added to the request and one to those were removed from that request.

What I tried to do was:

var remove = sendEmail('privateNote', toArray, '', '', refresh, obj);

// Trigger Email to those who are added to the request
// However, I was trying to send a the other email with params as a callback instead of refreshing the page.

sendEmail('privateNote', toArray, '', '', remove, obj);

The problem when doing this is that it seems to be firing both at the same time without waiting for one to finish causing some async issues.

Is there a way to do this correctly? I know this may not be the prettiest way to handles emails but everything has worked fine so far when only having to deal with one email at a time.

Rick Hitchcock

This immediately calls the sendEmail() function:

var remove = sendEmail('privateNote', toArray, '', '', refresh, obj);

Since sendEmail() doesn't return anything, remove is undefined.

To make it a proper callback, wrap it in a function():

var remove = function() {
  sendEmail('privateNote', toArray, '', '', refresh, obj);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Javascript: how to pass parameters to callback function

From Dev

Pass parameters to a url callback function in JavaScript

From Dev

how to add additional parameters to a callback function in javascript

From Dev

Where do the parameters in a javascript callback function come from?

From Dev

Javascript custom function with a callback parameter to be executed in a event listener with different parameters?

From Dev

javascript, passing parameters to a callback inside a closure that is inside a function

From Dev

Use a function with parameters inside a callback

From Dev

jQuery plugin callback function parameters

From Dev

Python Matplotlib callback function with parameters

From Dev

Node + Callback function pass parameters

From Dev

Javascript Callback with Parameters and Return Value

From Dev

JavaScript Multiple Callback Function

From Dev

Javascript array with callback function

From Dev

Usage of Javascript callback function

From Dev

javascript callback on function

From Dev

JavaScript: custom callBack function

From Dev

Javascript with callback function not working

From Dev

OOP with Javascript and callback function

From Dev

Javascript callback function not work

From Dev

Creating a callback on javascript function

From Dev

Javascript anonymous callback function

From Dev

javascript callback function selection

From Dev

Javascript Callback function malfunction

From Dev

Callback with arrow function in javascript

From Dev

Javascript Custom Callback Function

From Dev

Understanding parameters in an anonymous function (callback example)

From Dev

Accessing parameters passed to $.get into success callback function

From Dev

Understanding parameters in an anonymous function (callback example)

From Dev

How to add extra parameters to a function callback

Related Related

  1. 1

    Javascript: how to pass parameters to callback function

  2. 2

    Pass parameters to a url callback function in JavaScript

  3. 3

    how to add additional parameters to a callback function in javascript

  4. 4

    Where do the parameters in a javascript callback function come from?

  5. 5

    Javascript custom function with a callback parameter to be executed in a event listener with different parameters?

  6. 6

    javascript, passing parameters to a callback inside a closure that is inside a function

  7. 7

    Use a function with parameters inside a callback

  8. 8

    jQuery plugin callback function parameters

  9. 9

    Python Matplotlib callback function with parameters

  10. 10

    Node + Callback function pass parameters

  11. 11

    Javascript Callback with Parameters and Return Value

  12. 12

    JavaScript Multiple Callback Function

  13. 13

    Javascript array with callback function

  14. 14

    Usage of Javascript callback function

  15. 15

    javascript callback on function

  16. 16

    JavaScript: custom callBack function

  17. 17

    Javascript with callback function not working

  18. 18

    OOP with Javascript and callback function

  19. 19

    Javascript callback function not work

  20. 20

    Creating a callback on javascript function

  21. 21

    Javascript anonymous callback function

  22. 22

    javascript callback function selection

  23. 23

    Javascript Callback function malfunction

  24. 24

    Callback with arrow function in javascript

  25. 25

    Javascript Custom Callback Function

  26. 26

    Understanding parameters in an anonymous function (callback example)

  27. 27

    Accessing parameters passed to $.get into success callback function

  28. 28

    Understanding parameters in an anonymous function (callback example)

  29. 29

    How to add extra parameters to a function callback

HotTag

Archive