Nikhil salwe
Nikhil salwe

Reputation: 355

variable is not getting increases?

I am loading html pages on click of next button one by one in my Div tag as shown in below code.But as requirement I want to stop/break my loop after each page is load.but when I click again on next button my variable is not getting increase its value.

 function loadFiles() {

      var files = ['index2.html', 'index.html','index3.html','index4.html'];
      var i=1;
      while(i<=5)
      {
           alert(i+'')
           var file = files[i];

           $('#tabpage_1').load(file + '');

           document.getElementById('txtPageNo').value = i; 
           i++;
           break;

      }
 }

so I want to increase my variable after loop breaks.

Upvotes: 0

Views: 56

Answers (3)

sidneydobber
sidneydobber

Reputation: 2910

Just remove the break statement! The position of your i variable is where it should be. Moving it outside your function would set it for all functions in that scope, not very usefull! The break simply does what it's supposed to do which is breaking the loop! If you want to break the loop you must do this for a specific condition.

Upvotes: 0

Irfan TahirKheli
Irfan TahirKheli

Reputation: 3662

Define your variable outside your function so it doesn't initialize everytime.

 var i=1;
    function loadFiles() {

                var files = ['index2.html', 'index.html','index3.html','index4.html'];

                while(i<=5)
                {
                alert(i+'')
                var file = files[i];

                $('#tabpage_1').load(file + '');

                document.getElementById('txtPageNo').value = i; 
                i++;
                break;

                }

Upvotes: 1

PSR
PSR

Reputation: 40338

Because of break it is not updating.

Once the

document.getElementById('txtPageNo').value = i; 
           i++;

statements are get executed the loop is terminating because ofbreak.

Upvotes: 0

Related Questions