0

Is it possible to an call MVC action(Logout) from the jquery setTimeout function?

I have tried the following code:

setTimeout(function () { @Html.Action("Logout") }, 150000);
1
  • You can make an AJAX call to an MVC whatever and logout from there. Apr 29, 2015 at 21:19

1 Answer 1

2

Do you understand the difference between when code runs?

This code:

setTimeout(function () { @Html.Action("Logout") }, 150000);

Will produce something like this on the client:

 setTimeout(function () { <div><a href="">Logout</a></div> }, 150000);

Which is completely invalid javascript (regardless what it actually does it will return html normally).

You could do (I think this is right)

 setTimeout(function () 
   { 
     window.location = '@Url.Action("Logout","Account")'; 
   }, 150000);

Which would produce something like:

 setTimeout(function () 
   { 
     window.location = '/Account/Logout'; 
   }, 150000);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.