0

I'm creating a functionality that sets a time before a user is automatically logged out if he's been idle for that time. The user can choose this time itself, that's why I wrote the AJAX call.

Sidenote: I'm a Javascript/Jquery/... beginner.

function checkTimeBeforeIdle() {
alert("BEFORE: " + localStorage.getItem("timeBeforeIdle"));

    if (localStorage.getItem("timeBeforeIdle") === null || localStorage.getItem("timeBeforeIdle") == "undefined") 
    {
        localStorage.setItem("timeBeforeIdle", getTimeBeforeIdle());

        alert("AFTER: " + localStorage.getItem("timeBeforeIdle"));
    }
}

function getTimeBeforeIdle() {
$.ajax({
    url: 'Idle/GetTimeBeforeIdle',
    type: 'GET',
    dataType: 'json',
    success: function (data) {
        // process the data coming back
        alert("DURING: " + data);
        return data;
    },
    error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(thrownError);
    }
});

}

The alerts that I'm getting with this configuration are (in the same order):

BEFORE: undefined

AFTER: undefined

DURING: 15

The behaviour I expected was:

BEFORE: undefined

DURING: 15

AFTER: 15

From my C# 'experience' I thought the method should be awaited of some sort? Is this right, and if so, how do I do it? Or what way should this functionality be implemented.

I see that multiple, similar questions have already been asked but I don't really understand the answers there and how to use that withing my code. Maybe someone could explain the answer found here: How do I return the response from an asynchronous call? more concretely for me.

Community
  • 1
  • 1
Gravinco
  • 687
  • 1
  • 9
  • 28
  • JavaScript does not await. It works on promises. You should search and read up on asynchronous programming in JavaScript. – Brett Jan 27 '15 at 14:55
  • @Pete While searching for the answer I have come past a couple of post that say that I absolutely should NOT do that :-/ – Gravinco Jan 27 '15 at 14:59
  • @Brett I have tried that, using this link: https://api.jquery.com/promise/ but I don't fully understand how I could implement it in this code :-/ maybe you know some better documentation or do you have an example? – Gravinco Jan 27 '15 at 15:00
  • The problem is you are trying to run your code synchronously so unless you make a callback, your after alert will always end up running before the during alert (if the time taken to process is more). A better solution would be to do your ajax call inside your if function setting the localStorage on success and then have a new function for what you want to do if it is set and then call that function after you set the local storage in the ajax success and also in the else statement of the above if - [Example](http://jsfiddle.net/t9ea3zuL/) – Pete Jan 27 '15 at 15:21
  • @Pete Thanks a lot man, that did the trick :) I have been trying to create a callback function. I got it to 'work' but my 'return data' apparently just gave undefined.. Now I get the behaviour I expected. – Gravinco Jan 27 '15 at 15:55

0 Answers0