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.