简体   繁体   中英

How do I get JavaScript setTimeout to stop after 10 tries?

I have the following part of a jQuery .ajax call. It checks every second I believe. How can I get to stop after 10 tries?

 success: function (data) {
                    if (!data) {
                        setTimeout(function (inputInner) { CallIsDataReady(inputInner); }, 1000);
                    }

EDIT: I implemented one solution but the logging for the service call seems to stop after 2 times.

function CallIsDataReady(input) {
            var timer;
            var count = 0;

            $.ajax({
                url: "http://www.example.com/services/TestsService.svc/IsDataReady",
                type: "GET",
                contentType: "application/json; charset=utf-8",
                data: input,
                // dataType: "json",
                success: function (data) {
                    if (!data) {
                        setTimeout(function(inputInner) {
                            CallIsDataReady(inputInner);
                            count++;
                            if (count == 10) {
                                clearInterval(timer);
                                count = 0;
                            }
                        }, 1000);
                    }
                    else {
                        console.log("data returned - calling callUpDateGrid");
                        //Continue as data is ready
                        callUpdateGrid(input);
                    }
                },
                error: function (jqXHR, textStatus, errThrown) {
                    console.log("AJAX call failed in CallIsDataReady");
                    console.log(errThrown);
                }
            });

只需将setTimeout分配给一个变量,并使用一个计数器,当它达到10时,调用clearTimeout()

user setInterval with a counter:

if (!data) {
    var count = 0;
    var interval;
    interval = setInterval(function (inputInner) {
        if (count === 10) {
            clearInterval(interval);
            return;
        }
        CallIsDataReady(inputInner);
        count++;
    }, 1000);
}
  // global variable - should be outside your ajax function
 var timer;
 var count = 0;

 ....
 success : function( data) {
               if( ! data ) {
                  // don't know where you are getting inputInner. assuming its a global variable
                  timer = setInterval ( function ( inputInner ) { 
                                            CallIsDataReady(inputInner);
                                            count++; 
                                            if ( count == 10 ) { 
                                              clearInterval(timer); 
                                              count = 0;
                                            }  
                                        }, 1000);
                }
           }

Use an interval

var count = 0,
    handler = setInterval(dostuff, 1000);

function dostuff(inputInner){

   //CallIsDataReady(inputInner);

   if(++count === 10)
       clearInterval(handler);
}

http://jsfiddle.net/mGgLz/

However, you should never rely on the assumption that the ajax call always takes < 1000ms. Check the readyState on the XHR and make sure it's 4 before polling again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM