简体   繁体   English

图像加载时间延迟

[英]Image load time delay

This scripts works, it loads in my images. 该脚本有效,并加载到我的图像中。

I want this to load in a new images after say 10 seconds, when I try this it's not working any suggestions on what I have done wrong? 我希望在10秒钟后将其加载到新图像中,当我尝试执行此操作时,对我做错了什么没有任何建议?

    <?php header("Refresh: 5; URL=mytestfile.php"); ?>
<div style="width: 964px;">
<div style="float: left; width: 240px;">
<?php $mybanners[1] = '<a href="/chiropractor/"><img src="banners/chiropractor.jpg"></a>';
$mybanners[2] = '<a href="/chiropodist/"><img src="banners/chiropody.jpg"></a>';
$mybanners[3] = '<a href="/fitness-instructor/"><img src="banners/fitness ball.jpg"></a>';
$mybanners[4] = '<a href="/dietician/"><img src="banners/Dietician.jpg"></a>';
$mybanners[5] = '<a href="/alexander-technique/"><img src="banners/Alexander technique.jpg"></a>';

$id = rand(1,5);

echo $mybanners[$id]; 

// this is the section I tried to control the delay

$mybanners = 0;
for($i=0; $i<10000; $i++){
if($i=='10000'){
    $mybanners++;
    $i = 0;
    }
    if($mybanners=='10'){
    $mybanners=0;
 }

}?>

You can't control this via PHP, this is a Javascript use case. 您无法通过PHP进行控制,这是Javascript用例。 The for is not a time related control: http://php.net/manual/en/control-structures.for.php for不是与时间相关的控件: http : //php.net/manual/en/control-structures.for.php

You sould have a list of images in Javascript: 您可以使用Javascript获得图像列表:

var myImages = [
  '<a href="/chiropractor/"><img src="banners/chiropractor.jpg"></a>',
  '<a href="/chiropractor/"><img src="banners/chiropractor.jpg"></a>',
  '<a href="/chiropractor/"><img src="banners/chiropractor.jpg"></a>',
  '<a href="/chiropractor/"><img src="banners/chiropractor.jpg"></a>',
];

(you can generate this list with PHP of course) (您当然可以使用PHP生成此列表)

And then, every 10 seconds, do something with it: 然后,每10秒对它执行以下操作:

var i = 0;
window.setInterval(function() {
  alert(myImages[i]);
  i++;
, 10000}

PHP if for generating pages, there is nothing it can do after the page is served to the client. PHP如果用于生成页面,则在将页面提供给客户端之后无能为力。

If you're trying to cycle using the Refresh header, then the cycle shouldn't be needed. 如果您尝试使用“刷新”标头进行循环,则不需要循环。
Also, counting to 10000 does NOT take 10 seconds. 同样,计数到10000不需要10秒。 And you're using your former urls array as the index... In short, build it again. 并且您将以前的urls数组用作索引...简而言之,再次构建它。

Make the urls list available to javascript if you want to rotate them in the browser. 如果要在浏览器中旋转网址列表,请使用javascript。

edit: add example 编辑:添加示例

1) make urls available to javascript: 1)使url可用于javascript:

in php, you output the javascript: 在php中,您输出javascript:

echo "<script>";
echo "var urlList = new Array();";
foreach ($urls as $url)
  echo "urlList[urlList.length]='$url';";

2) rotate them This is the basic construct that will run function every 5 secs: 2)旋转它们这是每5秒运行一次功能的基本结构:

setTimeout(function() {}, 5000);

you need to figure out a way to do the rotation: either keep a counter for the index of the array or try a random: 您需要找出一种进行旋转的方法:为该数组的索引保留一个计数器,或者尝试使用随机数:

setTimeout(function() {
  var index = Math.round(Math.rand * urlList.length);
  document.getElementById('#myimg').src.href=urlList[index];
}, 5000);

Note: I'm writing this code here, it's not tested but it should be enough to get you going. 注意:我在这里编写此代码,它未经测试,但足以使您开始工作。 Once you get it going, consider using some jQuery.fade transition. 完成后,请考虑使用一些jQuery.fade过渡。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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