Latency problem on FF and safari and IE
so i've build a function to scan a dir based on a $_GET var, the code:
$dir="img/".$_GET["gal"];
$a=scandir($dir);
$b=array_slice($a,2);
for($i=1,$j=1;$i,$j<=count($b);$i++,$j++){
$marginRight=($i==6||$i==12||$i==18||$i==24||$i==30||$i==36)?"margin-right: 0":"margin-right: 13px";
if($i<10 && $j<10)
echo '<div class="GalThumbs" style="'.$marginRight.'"><a href="'.$dir.'/'.$_GET["gal"].'-0'.$j.'.jpg" rel="example3"><img src="'.$dir.'/'.$_GET["gal"].'-0'.$i.'.jpg"/>开发者_如何转开发</a></div>';
else{
echo '<div class="GalThumbs" style="'.$marginRight.'"><a href="'.$dir.'/'.$_GET["gal"].'-'.$j.'.jpg" rel="example3"><img src="'.$dir.'/'.$_GET["gal"].'-'.$i.'.jpg"/></a></div>';
}
/* if($i==11)
break; */
}
if there are more than ten images to scan a latency occurs(redraw of the browser slowish) i discover this using the commented:
if($i==11)
break;
if it's :
if($i==10)
break;
there will be no latency. don't understand what's going on here..
Let's take a look at your code, I think you are somehow lost in all that code and what it does. With a little bit of clean-up you should be able to debug your latency issue far easier.
Let's start with the for-loop:
for($i=1,$j=1;$i,$j<=count($b);$i++,$j++)
Both iterator variables are set in parallel. I'm sure you have thought about something when you wrote it, but this makes no sense. Try to reduce problems if you're unsure how to go on.
One variable does the job. Same here:
if($i<10 && $j<10)
And then I stumbled over this:
$marginRight=($i==6||$i==12||$i==18||$i==24||$i==30||$i==36)?"margin-right: 0":"margin-right: 13px";
You are actually looking for modulo: Get the remainder of $i
divided by 6
.
The if/else blocks both contain the same code. There is no need to use it at all.
Then I assume for debugging you want to quit the for-loop with break.
Next to that your code can benefit from indentation.
And the directory is actually only read to get the number of files, so the code can be reduced to get the count of files.
Let's try:
$gal = $_GET["gal"]; // FIXME sanitize the input
$dir = sprintf('img/%s', $gal);
$max = count(scandir($dir))-2;
for($i=1; $i<=$max; $i++)
{
$marginRight = ($i % 6 ? '13px' : '0');
$style = sprintf('margin-right: %s', $marginRight);
$src = sprintf('%s/%s-0%d.jpg', $dir, $gal, $i);
echo '<div class="GalThumbs" style="', htmlspecialchars($style), '">',
'<a href="', htmlspecialchars($src), '" rel="example3">',
'<img src="', htmlspecialchars($src),'"/>',
'</a>',
'</div>'
;
// FIXME debug
/* if($i==11) break; */
}
精彩评论