开发者

ServerTime with Jquery

I need a js code for alway开发者_JAVA技巧s getting the server time in a webpage. This code is working but not get the time dynamically. Code gets time one time while webpage loading.

$(document).ready(function() {

               $.ajax({
                    type: "POST",
                    url: "GenericWCF.svc/GetSystemTime",
                    data: "{}",
                  contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(bs) {
                        $("#systemTime").text(bs.d);
                  }
               });

});


You need to use the window.setInterval method to call your function which performs your ajax call.

The window.setInterval method calls a function every X milliseconds repeatedly, until a call to window.clearInterval is called. Note that window.setInterval is "standard" javascript rather than jQuery.

For example:

window.setInterval('yourfunction()', 1000);

function yourfunction()
{
   // your ajax call here.
}

Will call your ajax function every 1000 milliseconds (every second).


As @CraigTP said you have to use a timer to continuously update the time.

But why using ajax to get the time? It'll be better to just get the server time upon rendering, and then update it in the client every minute, without ajax. Just add to it a minute every minute.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜