php - 显示图像并运行脚本十五秒

标签 php javascript jquery

这与我前几天提出的问题非常相似,但我的页面代码变得更加复杂,我需要重新审视它。我一直在使用以下代码:

$('#myLink').click(function() {
  $('#myImg').attr('src', 'newImg.jpg');
  setTimeout(function() { $('#myImg').attr('src', 'oldImg.jpg'); }, 15000);
});

单击链接时在设定的时间段(15 秒)内替换图像,然后在 15 秒后恢复为原始图像。

但是现在,我想在单击链接时也运行一段 javascript(除了替换图像),并且仅在单击链接时(它与 15 秒图像相关)然后让 js 代码在 15 秒后也消失了……但是我不确定如何让 jquery 将 js 代码发送到页面中……基本上我只想让 jQuery 将这段代码“回显”到 15 下的页面上第二,当我在那里时,但我不知道 jquery 是如何格式化这个“echo ”的。

这个问题有意义吗?

  interval = 500;
  imgsrc = "webcam/image.jpg";

  function Refresh() {
     tmp = new Date(); 
     tmp = "?" + tmp.getTime();
     document.images["image1"].src = imgsrc + tmp;
     setTimeout("Refresh()", interval);
  }

刷新(); 这是网络摄像头的脚本。基本上它每半秒拍摄一张新图片并在页面上替换它。你必须原谅我,我是 jquery 的新手,我不知道如何让这个脚本在 jquery 上下文中工作。

对不起,我解释得不好。这是我需要一步一步发生的事情:

  1. 用户来到该页面,并且有一个静态图像显示“单击此处查看网络摄像头”
  2. 用户点击图片
  3. 图像被实时网络摄像头图像替换,我问题中的第二个脚本每 .5 秒刷新一次图像。
  4. 15 秒后,实时网络摄像头恢复为静态图像,显示“单击此处查看网络摄像头”

只有在这 15 秒的时间间隔内,我才会运行网络摄像头刷新脚本,否则它会在甚至未显示的元素上浪费带宽。抱歉造成混淆。

最佳答案

这应该可以解决您的问题...(尽管您可能会尝试检测他们是否离开了页面(我无法就如何做到这一点向您提供任何建议))

var myLink = $('#myLink');
var myImg = $('#myImg');

myLink.click(function() {
   // makes sure this can only run once per 15sec
   myLink.attr('disabled', 'disabled');

   var start = new Date();
   var interval = null;

   function refresh() {
      var current = new Date();
      if (current.getTime() - start.getTime() > 15000) { // max time in miliseconds
          //stops refresh
          clearInterval(interval);

          // lets them click on the link again
          myLink.attr('disabled', '');

          // resets the old img
          myImg.attr('src', 'oldImg.jpg');
      } else {
          // should be obvious
          myImg.attr('src', 'webcam/image.jpg?t=' + current.getTime()); 
      }
   }

   // refreshes page
   interval = setInterval(refresh, 500);

   refresh(); // don't wait for the first 1/2 second
});

关于php - 显示图像并运行脚本十五秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2940284/

相关文章:

javascript - 使用 Sails.js 进行个性化聊天

php - 从 MySQL 表发布日期信息

php - 通知 : Array to string conversion with date

javascript - 如何从用户输入中获取两个位置之间的距离

javascript - 我如何在 JQuery 中执行此操作? (获取 AJAX-GET 返回的 HTML 并将其转换为对象)

javascript - 如何在 Adob​​e Dreamweaver CS4 上使用 javascript

javascript - 在网页上自定义 Twitter 推文

php - Zend 框架 : What are the differences between init() and preDispatch() functions in controller objects?

php - Eloquent/Laravel : is there a counterpart to object->load ('relation' ), 例如。对象-> 卸载 ('relation')?

javascript - 从文件路径创建blob vue electro builder