문제

I have a problem with the code below. Basically im implementing a polling mechanism, but when it is supposed to stop polling it just continues to poll. Polling infinite.

Is there any other way to force it to stop?

The function SearchPoll is only called once.

function SearchPoll(){
    var i = setInterval(function () 
    { 
        var url = "Search.aspx?polling=true";        
        $.ajax( 
        { 
            url: url,
            success: function (data) 
            {     
                clearInterval(i); 
            }, 
            error: function () 
            { 
                clearInterval(i); 
            } 
        }); 
    }, 1000); 
}
도움이 되었습니까?

해결책

If you want to kill all requests you need to work with the XMLHTTPRequest object returned from $.ajax. Here is an example:

function SearchPool(){
    var xhrArray = [], 
        interval = setInterval(function () { 
            var url = "Search.aspx?polling=true";        
            xhrArray.push($.ajax({ 
                url: url,
                success: function (data) {     
                    for(var i = 0; i < xhrArray.length; i+= 1) {
                        xhrArray[i].abort();
                    }
                    clearInterval(interval); 
                }, 
                error: function () { 
                    clearInterval(interval);
                } 
            })); 
        }, 20);
}
SearchPool();

다른 팁

This fiddle seems to work correctly... http://jsfiddle.net/sramam/yZTAK/

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top