jQuery: setInterval

前端 未结 4 1013
日久生厌
日久生厌 2020-12-17 18:58

I want to make a slideshow on my site, but the problem is that the setInterval works only one time. It loads my file only one time end then stops.

Heres the code: m

4条回答
  •  情话喂你
    2020-12-17 19:45

    The problem is probably that the browser has loaded random.php into the cache. I think you'd be better off giving each image it's own url and using that. Then a php page can present just the src:

    var refreshId = setInterval(function () {
      $.get('random-src.php', function (result) {
        $('#center>img').attr('src') = result;
      },'text');
    },5000);
    

    Then random.php could return img/img1.jpg, img/img2.jpg, etc at random.

    Alternatively, you could stick header("Cache-Control: no-cache, must-revalidate"); towards the top of your random.php file to prevent the browser from caching.

提交回复
热议问题