简体   繁体   English

如何使用JavaScript每30秒重新加载异地图像源?

[英]How to reload offsite image sources every 30 seconds with JavaScript?

I have some images from another source that need to refresh from their offsite source every 30 seconds. 我有一些来自其他来源的图像,需要每隔30秒从其异地来源刷新一次。 I would like to use JavaScript to accomplish this so as to avoid an entire page reload. 我想使用JavaScript来完成此操作,以避免整个页面重新加载。

Presently I've attempted something similar to this question: " reloading a page after every 10 sec in rails 3.1 " 目前,我已经尝试过与该问题类似的事情:“ 在Rails 3.1中每10秒钟后重新加载页面

( This is a Rails application, but I probably don't need a Rails specific answer in this case .) 这是一个Rails应用程序,但是在这种情况下,我可能不需要特定于Rails的答案 。)

Notwithstanding, I am ending up with no appreciable result when I add a div around the link + image nor when I add a div to the image itself. 尽管如此,当我在链接+图像周围添加div或将div添加到图像本身时,我最终都没有得到明显的结果。 I have attempted both solutions in this example by creating a element-reload.js. 在此示例中,我通过创建element-reload.js尝试了两种解决方案。

The first solution that's marked as the answer simply reloads the page with nearly all of the page elements absent. 标记为答案的第一个解决方案只是在几乎没有页面元素的情况下重新加载页面。 The second solution makes the image that I'm trying to refresh actually disappear upon first refresh when I surround the link + image with a div, but when I place the id upon which it's acting on the actual image tag, it yields nothing. 第二种解决方案使我要刷新的图像实际上在我用div包围链接+图像时在第一次刷新时实际上消失了,但是当我将其作用于其上的id放置在实际的图像标签上时,它什么也没有产生。

I'm sure I'm missing something rather simple since JS is not a strong suit for me at the moment. 我确定我会错过一些简单的事情,因为JS目前不适合我。

Finally, I do have a number of sources to refresh and would like to see an example of performing this for a class vs an id if possible, but having more granular control over each one may be best in the end for varied times for the refreshes. 最后,我确实有很多要刷新的资源,并且想看一个在可能的情况下针对类和ID执行此操作的示例,但是最终最好在每个时间都进行更细粒度的控制,以获得不同的刷新时间。

If you're up for jQuery , this can be done quite easily: 如果您打算使用jQuery ,那么可以很容易地做到这一点:

$(function() {
  setInterval(function() {
    $('img').each(function() {
      $this = $(this);

      $this.attr('src', $this.getAttribute('src') + '?timestamp=' + new Date().getTime());
      console.log($this.prop('src'));
    });
  }, 30 * 1000);
});

In order to prevent browser caching, you have to fool the browser and load the image with a GET request variable timestamp . 为了防止浏览器缓存,您必须使浏览器傻瓜并使用GET请求变量timestamp加载图像。 It doesn't matter what the parameter is, but the image will load brand-new and not from cache because the URL changes. 参数是什么都没有关系,但是图像会加载全新的图像,而不是从缓存加载,因为URL会更改。


jQuery is famous for its use of CSS-like selectors. jQuery因使用类似CSS的选择器而闻名。

Replace $('img') with one of these: $('img')替换$('img')以下之一:

$('img.yourClassName'); // Class
$('#your_id, #another_id, ...'); // ID(s). Omit the comma for a single id
$('img[id^="common_base_id"]'); // Selects all images with an id that starts with "common_base_id".

There's also the :not() selector, which can filter your results: 还有:not()选择器,它可以过滤结果:

$('img.yourClassName:not(.do-not-reload)');
$('img.yourClassName:not([src="img/spinner-skip.gif"])');

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

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