带有回调和参数的Javascript函数

带有回调和参数的Javascript函数,javascript,jquery,jquery-callback,Javascript,Jquery,Jquery Callback,我在尝试添加回调时遇到了一些基本JS函数的问题,该回调需要使用params运行另一个函数 这是我的电子邮件功能: function sendEmail(template, to, cc, bcc, callback, optional=null){ // Define vars needed var body = '', subject = ''; // Based on the template.. switch(template){ case 'privateNote'

我在尝试添加回调时遇到了一些基本JS函数的问题,该回调需要使用params运行另一个函数

这是我的电子邮件功能:

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: "noreply@domain.com",
        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');
}
sendEmail('privateNote', toArray, '', '', refresh, obj);
var remove = sendEmail('privateNote', toArray, '', '', refresh, obj);
正如预期的那样,这一切都很好,但我面临一个问题

有一个部分,我需要同时发送两封电子邮件,一封给添加到请求中的人,一封给从该请求中删除的人

我想做的是:

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);
这样做的问题是,它似乎在不等待一个完成的情况下同时启动了这两个程序,从而导致了一些异步问题


有没有正确的方法?我知道这可能不是处理电子邮件的最佳方式,但到目前为止,当一次只处理一封电子邮件时,一切都很顺利。

这会立即调用
sendmail()
函数:

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: "noreply@domain.com",
        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');
}
sendEmail('privateNote', toArray, '', '', refresh, obj);
var remove = sendEmail('privateNote', toArray, '', '', refresh, obj);
由于
sendmail()
不返回任何内容,
remove
undefined

要使其成为正确的回调,请将其包装在
函数()中。


这会立即调用
sendmail()
函数:

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: "noreply@domain.com",
        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');
}
sendEmail('privateNote', toArray, '', '', refresh, obj);
var remove = sendEmail('privateNote', toArray, '', '', refresh, obj);
由于
sendmail()
不返回任何内容,
remove
undefined

要使其成为正确的回调,请将其包装在
函数()中。