Javascript - 在FOR LOOP中以特定间隔执行某些操作

I got a problem while developing javascript based idea..

Here's the main code,

 $.post("datainlines.php", function(data) { 

        var lines = data.split("
");

           for (var i = 1; i < lines.length; i++) {

             sitename= lines[i]; sitetype=lines[i++]; 

             if(i/2 == '0'){ alert current values in sitename and sitetype variables}

            }

       } );

This will get data in datainlines.php via POST, datainlines.php file data structure is like this -

Google
search
Stackoverflow
Questions
Yahoo
search
Facebook
social network

How to get site name and type in groups, like

Google
search 

then

Stackoverflow
questions

is there anyway to group out and alert per every two loops in for loop ?

Increment your loop value by two:

$.post("datainlines.php", function(data) {

    var lines = data.split("
");

    for (var i = 1; i < lines.length; i+=2) {
        sitename = lines[i];
        sitetype = lines[i+1];
        alert(sitename + " " + sitetype);
    }
});​

Note the i+=2 in the for loop. This will skip every other entry in the array.


As an aside, I think with this line:

if(i/2 == '0')

What you were actually looking for is the modulus:

if(i % 2 === 0)