简体   繁体   English

在javascript中以时间间隔切换选中的复选框

[英]switch checked check-box with time interval in javascript

I'm a total noob at javascript and currently having issues with a project.我是 javascript 的完全菜鸟,目前在项目中遇到问题。

I'm trying to make a picture slide with specific transitions.我正在尝试制作带有特定过渡的图片幻灯片。 The slide builds on and tags for the transition (check-boxes).幻灯片建立在过渡的标签上(复选框)。 I tried making a script that changes the checked box after every 5s.我尝试制作一个脚本,每 5 秒更改一次复选框。

I already got a good chunk of help from a friend but I can't get it to work at all.我已经从朋友那里得到了很多帮助,但我根本无法让它发挥作用。

This is the script:这是脚本:

   var slider_i = 1; 
   function slider_change(){
      if(++slider_i > 4){
         slider_i=1;
      } 
      document.getElementById('select-img-'+slider_i).checked = true;         
      setTimeout(slider_change, 5000);
   } 
   slider_change();

Here is the slider I'm talking about: http://demo.br-photography.ch/Portal/transitions/index_3.html这是我正在谈论的滑块: http : //demo.br-photography.ch/Portal/transitions/index_3.html

I made a jsfiddle, easier to test cause then we can see if the checkboxes without the css properties actually switch.我做了一个 jsfiddle,更容易测试原因然后我们可以看到没有 css 属性的复选框是否实际切换。 They do, but my slide on the testsite is not switching the images as it should...?他们这样做了,但是我在测试站点上的幻灯片没有按应有的方式切换图像......?

https://jsfiddle.net/t4eapmgb/ https://jsfiddle.net/t4eapmgb/

try:尝试:

   var slider_i = 0;    // !!!
   var slider_change = function (){
      if(++slider_i > 4){
         slider_i=0;
      } 
      document.getElementById('select-img-'+slider_i).checked = true;         
      setTimeout(slider_change, 50000);
   };
   slider_change();

or或者

   var slider_i = 1;    
   var slider_change = function (){
      if(slider_i++ > 3){ // !!!
         slider_i=1;
      } 
      document.getElementById('select-img-'+slider_i).checked = true;         
      setTimeout(slider_change, 50000);
   };
   slider_change();

what's actually happening with your code:您的代码实际发生了什么:

  • slider_i is set to 1. slider_i设置为 1。
  • function slider_change is called函数slider_change被调用
  • it first increments the slider_i because of ++slider_i它首先递增slider_i因为++slider_i
  • now slider_i has the value of 2现在slider_i的值为2
  • it now checks against <4它现在检查<4
  • if not true, it checks an element with id select-img-<value of slider_i >如果不是 true,它会检查一个 id 为 select-img-< slider_i值 > 的slider_i

The last step generates the id of select-img-2 .最后一步生成select-img-2的 id。

select-img-1 will never be reached. select-img-1永远不会到达。 I don't know whether this is causing any subsequent errors.我不知道这是否会导致任何后续错误。

For your information, setTimeout(, 50000) is triggerd after 50000 ms = 50sec.供您参考, setTimeout(, 50000) 在 50000 ms = 50sec 后触发。 If you want to wait 5 sec you have to write setTimeout(, 5000)如果你想等待 5 秒你必须写 setTimeout(, 5000)

Just place script from top to bottom.只需将脚本从上到下放置。 There is reference error because elements(checkboxes) are not available when initially page is loaded.由于初始页面加载时元素(复选框)不可用,因此存在引用错误。

Or you may also add try catch block for this issue as follows:或者你也可以为这个问题添加 try catch 块,如下所示:

replace this替换这个

var slider_i = 1; 
function slider_change(){
 if(++slider_i > 4){
   slider_i=1;
 } 
 document.getElementById('select-img-'+slider_i).checked = true;         
 setTimeout(slider_change, 5000);
} 
slider_change();

With this one:-有了这个:-

var slider_i = 1; 
function slider_change(){
  if(++slider_i > 4){
    slider_i=1;
  } 
  try{
    document.getElementById('select-img-'+slider_i).checked = true;         
  }catch(err){}
  setTimeout(slider_change, 5000);
} 
slider_change();

Use

//Repeats forever
var variable = setInterval(functionYouWantToCall, 5000);
//Cancel the interval
clearInterval(variable);

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

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