开发者

How do I build a specialized JQuery Timer

I have an asp.net page where two grid views are available showing current stock market price updates. I need to update these two grid views every 20 seconds. So I was thinking of using JQuery to do this job.

What I need is a timer which will fire every 20 sec开发者_运维问答onds, send an ajax request to the servers, bring the order lists from the server using json and then update those two grid views. If a request to the server is still being served 20 seconds after it is fired, then I want the old one to be aborted without causing any trouble.

I already know how to bring objects using json. I just need to figure out how to send a request every 20 seconds and cancel a request if it is still being server for 20 seconds.


Use javascript's setInterval() to run some code at an interval.

jQuery's $.ajax() will return the XMLHTTPRequest object that you can abort.

var request;    // Stores XMLHTTPRequest object

setInterval(function() {
                 // if there's a current request, abort
              if(request) request.abort();

                // make ajax request, and assign request to variable
              request = $.ajax({
                     // My ajax request parameters
              });

}, 20000);       // repeat every 20 seconds (20,000 milliseconds)

http://api.jquery.com/jQuery.ajax/


EDIT:

If you ever need to stop the interval from running, you need to assign the interval to a variable, and then clear it whenever you want.

var theInterval = setInterval(function() {...}, 20000);

theInterval.clear();   // To stop the interval from running


Use javascript's

setTimeout("doSomething()",1000);


Use jquery.timers-1.0.0.js

and this method will work fine if we use JSON for posting and getting data

function setTimer() {

        $(function () {


                    $.ajax({ url: 'url', //url which is requested
                        type: 'POST',
                        data: json,
                        dataType: 'json',
                        contentType: 'application/json; charset=utf-8',
                        success: function (data) {
                           // do something
                        }

                    })
                });

    }





    function resetTimer() {

        $(function () {

                $.ajax({ url: 'url', //url which is requested
                    type: 'POST',
                    data: json,
                    dataType: 'json',
                    contentType: 'application/json; charset=utf-8',
                    success: function (data) {
                        // do something
                    }

                })
            });

    }

in this the url can be the view where the required result is displayed.

The setTimer() and resetTimer() methods can be used to set the action needed and reset the action according to any trigger like a button press


Use icallbackeventhandler with the timer

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜