简体   繁体   中英

Automatic action result call from Javascript MVC

I am working on a website and I am wanting to redirect to Action Result called logout automatically in 20 second.

This is what I have so far:

setInterval(function redirectAction() {

    var url = '@Url.Action("Logout")';

}, 20000);

The timing system works. I tired using alert and it alerts every 20 seconds. BUT when it starts after page load, it starts with 20 second delay and then alerts which is important because I need user to be in one page for 20 seconds then logouts.

Overall, what I'm trying to achieve is how to automatically call an action result from controller using javascript. I've tried researching GET method and AJAX, but still haven't able to find a clear solution and nothing worked so far.

just do this :

setInterval(function redirectAction() {

window.location.href ="your url you want to redirect to it"

//example
//window.location.href ="YourController/logout"

}, 20000);

also you can use setTimeout for delay like this :

 setTimeout(function () {
       window.location.href = "YourController/logout"
    }, 2000);

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