Search code examples
asp.net-mvcasp.net-mvc-ajax

Trigger an asp.net mvc ajax refresh automatically every X minutes


I have a Refresh button on my page that will perform an asp.net mvc ajax post to update some of the content on the page. The refresh/update process can be long running (about 10-20 seconds). Right now the user has to manually click the Refresh button to trigger the refresh. I want to trigger the refresh automatically every X minutes. Pretty much I want to trigger this same Ajax post as below (and it would be nice to display the LoadingElementId too).

<% using (Ajax.BeginForm("Refresh", null, 
                   new {userId=Model.UserId},
                   new AjaxOptions { UpdateTargetId = "UserForm", LoadingElementId = "RefreshingDiv" },
                   null))
               {
            %>

                    <button type="submit">
                        <img src="/zsys/img/icons/refresh.png" alt="Refresh" />
                        Refresh
                    </button>

            <% } %>

How can I force the asp.net mvc ajax postback?


Solution

  • Wyatt Barnett's answer made me try just clicking the Refresh button for the user. And then I used setInterval to trigger the Refresh Button click every 5 minutes:

        <script src="../../scripts/jquery-1.3.2.min.js" type="text/javascript"></script>
    
        <script type="text/javascript">
            $(document).ready(function() {
                setInterval("$('#RefreshFormButton').click()", 300000);
            });
        </script>