简体   繁体   中英

JavaScript: function () not executed when redirecting to another page after it

I have functions

AddActivity(myCallback);
sendMailToOrgs();
sendMailToMember();

being called one after the other. works fine every time. However I want the 2 sendMail...() functions to be invoked only when AddActivity(myCallback) is successfull. So I place the two function calls into the myCallback() function which is triggered when AddActivity(myCallback) is successfull.

function myCallback(response)
    {        
        if (response > 0)
        {
            sendMailToOrgs();
            sendMailToMember();
            window.top.location = "http://www.mywebpage.ch/index.php?page_id=3292";
        }
    }

However: senMailToOrgs() is working fine every time. sendMailToMember() works only like every second time. When I change the order and put sendMailToMember() first and sendMailToOrgs() second then sendMailToMember() works every time and the other one only every second. When I comment out the window.top.location.... both mails are sent.

the two functions send data to a php file which invokes a function which sends an e-mail they are not taking any callback. they both look almost the same:

function sendMailToMember()
{
    var data = "eventName=" + "<?php echo $event['name'];?>" + "&userEmail=" + "<?php echo $_SESSION['user'];?>" + "&first=" + "<?php echo $_SESSION['first']; ?>" + "&last=" + "<?php echo $_SESSION['last']; ?>";

    $.ajax({
        dataType: 'json',
        url: 'stcg-json-responses.php?fct=emailSendToVolunteer',
        data: data,
        cache: false
    });
}

I found a workaround where I do not invoke the redirection code directly after invoking the two sendMail...() functions like here:

sendMailToOrgs();
sendMailToMember();
window.top.location = "http://

but instead I add a Callback() function to sendMailToMember() which contains the redirection

window.top.location = "http://

downside of this approach is that after I click the Submit button nothing happens for a bit more than two seconds and the user has enough time to click a second time which can cause that the AddActivity(Callback) function mentioned in the question gets executed and returns an error. And then a myCallbackError() function is invoked which redirects the user to another site with an error message.

now I solved the issue by moving the redirection code into a function and then delaying the function

 function redirect()
    {
        if (response > 0)
    {
        window.top.location = "http://mywebpage.ch/index.php?page_id=3292";
    }
};
setTimeout (redirect,1500);

However I still do not understand why the problem appeared: 1. Why does the redirection of a webpage in the users browser stop a php-script which is being executed on the server? 2. and why does the problem appear only every second time? its almost as if the function gets started but is then stopped in the middle and that the execution is finished the second time the function is called. is sth like that possible?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM