最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Stop Long Polling Function in JS - Stack Overflow

programmeradmin1浏览0评论

I have a page where I have implemented a long polling function to check the timestamp of a record, so it will update if the data has been edited.

<script type="text/javascript">
var poll_url = '/;
var my_ts = <?php $_SESSION['template_timestamp']; ?>;
(function poll(){
    $.ajax({ 
        url: poll_url,
        type: 'post',
        success: function(data){
            if ((data.ts > 0) {
                // check if expired
                if (data.ts > my_ts) {
                    // it has expired, so reload the page
                    $("#dialog-message").html('Record has been edited.');
                    // show popup
                    $("#dialog-message").dialog({
                        modal: true,
                        buttons: {
                            Reload: function() {
                                $(this).dialog("close");
                                $('#pleaseWait').show();
                                window.location.href = '/records/overview';
                            }
                        }
                    });
                }
            } else {
                // is still null
                console.log('error');
            }
        }, 
        dataType: "json", 
        plete: poll, timeout: 30000 
    });
})();
</script>

The problem I have is that there is also another action which invokes a JS ajax call, which when called I would like to force the long polling function to stop.
Is there a way to do this?

I have a page where I have implemented a long polling function to check the timestamp of a record, so it will update if the data has been edited.

<script type="text/javascript">
var poll_url = 'http://mysite./poll/;
var my_ts = <?php $_SESSION['template_timestamp']; ?>;
(function poll(){
    $.ajax({ 
        url: poll_url,
        type: 'post',
        success: function(data){
            if ((data.ts > 0) {
                // check if expired
                if (data.ts > my_ts) {
                    // it has expired, so reload the page
                    $("#dialog-message").html('Record has been edited.');
                    // show popup
                    $("#dialog-message").dialog({
                        modal: true,
                        buttons: {
                            Reload: function() {
                                $(this).dialog("close");
                                $('#pleaseWait').show();
                                window.location.href = '/records/overview';
                            }
                        }
                    });
                }
            } else {
                // is still null
                console.log('error');
            }
        }, 
        dataType: "json", 
        plete: poll, timeout: 30000 
    });
})();
</script>

The problem I have is that there is also another action which invokes a JS ajax call, which when called I would like to force the long polling function to stop.
Is there a way to do this?

Share Improve this question asked Oct 22, 2012 at 9:53 RooneylRooneyl 7,9026 gold badges56 silver badges82 bronze badges 2
  • The following SO question may provide the answer... stackoverflow./questions/446594/… – Stuart Wakefield Commented Oct 22, 2012 at 9:58
  • @StuartWakefield ; thanks fot that. Didn't find that snippet. If you post that ment as an answer I will accept it. – Rooneyl Commented Oct 22, 2012 at 10:24
Add a ment  | 

1 Answer 1

Reset to default 5

The following SO question may provide the answer... Abort Ajax requests using jQuery

Applying that to your code snippet, you might get something along the lines of...

<script type="text/javascript">
var poll_url = 'http://mysite./poll/';
var my_ts = <?php $_SESSION['template_timestamp']; ?>;
var poll_xhr;

(function poll(){
    poll_xhr = $.ajax({ 
        url: poll_url,
        type: 'post',
        success: function(data){
            // Code removed
        }, 
        dataType: "json", 
        plete: poll, 
        timeout: 30000 
    });
})();

// To kill the AJAX request, put this where it makes sense
poll_xhr.abort();
</script>
发布评论

评论列表(0)

  1. 暂无评论