guihknx
guihknx

Reputation: 11

How increment an variable every time ajax success?

I am creating a system based on an unstable source ie I monitor every time my application can access this source via ajax successfully.

I created a variable and could not understand how I can increment it each time the success is true.

for example

        var i; // my int I want increment every time ajax status is success
        i=0;

        var  req = function(){
            jQuery.ajax({
                success: function(){
                    // but when I'm here i having some problems...

                    //i += i; ? = undefined
                    //i++; ? = 0
                    //i = i+1; ? = 1

                    // I wanted and expected something like => 0, 1, 2, 3, 4
                    //then.. what to do?
                },
                url:'/mysource/',
                type: 'POST',
                data: this.postfields,
            },function(e){});   
        }

            for( var o = 0; o<5; o++ )
                req();

Upvotes: 0

Views: 7302

Answers (2)

Idan Magled
Idan Magled

Reputation: 2216

i think that your var i is inside a function. you need to set it as a global var. if its already is global - its should work fine.

Upvotes: 0

helion3
helion3

Reputation: 37381

Create a variable outside of the ajax function and increment it each time:

var successCount = 0;
$.ajax(url, function(resp){
  successCount++
});

Upvotes: 2

Related Questions