1

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.

0

1 Answer 1

4

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.