user6446331
user6446331

Reputation:

Detect if a function was executed

I have the following function:

function enviarMail() {
    for (var x = 1; x <= idShort; x++) {
        var nume = $('#Num' + x).text();
        var purp = $('#Pur' + x).text();
        var open = $('#Open' + x).text();
        var close = $('#Close' + x).text();
        var owner = $('#Owner' + x).text();
        var fam = $('#Fam' + x).text();
        var vs = $('#Vs' + x).text();
        var sta = $('#Sta' + x).text();
        var mail = $('#Mail' + x).text();    

            $.ajax({
                type: 'POST', //POST | GET
                url: urlServer + 'ws_alerts.asmx/SendMailExp',
                data: '{"us":"' + owner +
                    '","email":"' + mail +
                    '","vs":"' + vs +
                    '","fam":"' + fam +
                    '","pur":"' + purp +
                    '","open":"' + open +
                    '","close":"' + close +
                    '","id":"' + nume + '"}', 
                dataType: 'json',
                contentType: 'application/json',
                timeout: 600000,
                error: function (xhr) {
                    bootbox.alert("No se pudo enviar el correo.");
                },
                success: function (data) {

                }
            });
    }
}

I call this function on (document).ready, works fine! but I only want to execute this function 1 time for example I log in at 8:00am the function is going to be executed, then if I log in again at 9:15am the function going to be executed again and I do not want my email full of this mail, so my question is how can I do to execute the function 1 time per day.

PD. Function is executed on main page, so a lot of users can execute the function, but I only want to execute it 1 time doesn't matter which user do it.

PD2. I was thinking in a timer but, the users can access from any device, so I don't know if this can work (for example I log in from my PC and the timer starts, then I log in from my Tablet and what happen with the timer? I'm going to have another time of it's going to be the same from PC?)

Upvotes: 0

Views: 82

Answers (2)

nikjohn
nikjohn

Reputation: 21850

What you're asking for needs to be enforced from the server side. As clients can log in (like you mentioned) at any time from any device, it is not possible to maintain this on the client side.

What you want is to somehow maintain state across all clients. You need to use cookies or session to do this, as these provide sharing of state between different clients and the server. As there is only one server(theoretically), it can be programmed to be aware whether an operation has been performed for a user in a day. This flag can be then shared across to all clients through session/cookie

Note: This is all assuming that you have some kind of authentication/session that can be shared between the server and your client. Otherwise, there is no way that the server can recognize you when you open the client form different devices/browsers

The process can be as follows:

  • The user logs in from ClientA. On the server side, (either in a in-memory store like Redis or Memcache, or in a DB), the server checks for the user's id to see if the functionTriggered is true already. If no, in the auth response, the server sends the functionTriggered=false flag (sets a cookie on the client)
  • ClientA makes the AJAX call and the server sets functionTriggered=true in the store
  • The user logs in from ClientB. Upon login, the server sees that functionTriggered=true, therefore, sends this flag. Server sets a cookie indicating this. The client sees this and skips the AJAX request.

Upvotes: 2

Picko
Picko

Reputation: 148

I agree with @DustinToothless but in contrary to what Dustin said I would suggest making a separate call (to keep things separate and maintain software engineering principals) to the server requesting whether it is OK to send the AJAX call or not. The server, however, would make the decision based on whether it had previously received a request or not -- that can accomplished by using a database or using a file comparing the last modified date (this way you can also log) -- and if it already received a request then the server would give the OK to execute the AJAX call.

Although, the above would work but it not efficient or secure since the client could potentially send the AJAX request regardless of what the server says (i.e. a hacker could cause a DoS attack) therefore, you should only use one function (this call even) and let the server decide (using the method above) whether to send the email or not.

Upvotes: 1

Related Questions