Currently, I have a page that updates data each 15 seconds. Depending on the time of day the data is being accessed, the update process could take longer than the 15 seconds when the next update should run. I've done some testing and it looks like when the page gets behind the 15 second timer, the page just keeps updating after the last one finishes until it catches up to itself.
Is there any way to make the page wait 15 seconds between each update, no matter if the update has taken 6 seconds or 30 seconds? Thanks!
Hi,
It can be achieved with window.setInterval method. You can start a asynchronous call in this method, and it waits for the specified time again and then triggers the function again.
Please try this demo:
<%@. Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"> [System.Web.Services.WebMethod] public static string getTime() { System.Threading.Thread.Sleep(2000); return DateTime.Now.ToString(); }</script><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"> <title>Untitled Page</title></head><body> <form id="form1" runat="server"> <div> <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"> </asp:ScriptManager> </div> <input type="text" id="time" /> <script type="text/javascript"> window.setInterval(getTime, 3000); function getTime() { PageMethods.getTime(onComplete); } function onComplete(result) { $get("time").value = result; } </script> </form></body></html>
Hope this helps.
No comments:
Post a Comment