Execute WebMethod (PageMethod) on every minute using using AJAX and JavaScript in ASP.Net

Last Reply 2 months ago By dharmendr

Posted 2 months ago

hi 

How to to call web method on every 5 minutes. How can I do this with javascript ?

Posted 2 months ago

Hi Shahzad786,

Use SetInterval function.

HTML

<asp:ScriptManager runat="server" EnablePageMethods="true" />
<script type="text/javascript">
    window.onload = function () {
        // Set interval. Currently it is 1 sec.
        setInterval(ShowMessage, 1000);
    }
    function ShowMessage() {
        PageMethods.Message(Success, Failure);
        function Success(result) { alert(result); }
        function Failure(error) { alert(error); }
    }
</script>

C#

[System.Web.Services.WebMethod]
public static string Message()
{
    return "Welcome to ASPSnippets.com" + "\n\r" + DateTime.Now;
}

VB.Net

<System.Web.Services.WebMethod()>
Public Shared Function Message() As String
    Return "Welcome to ASPSnippets.com" & vbLf & vbCr & DateTime.Now
End Function

Screenshot