简体   繁体   English

基于倒数计时器的最佳性能Javascript条件

[英]Best performance Javascript conditionals based on countdown timer

I am currently working on a project that is showing and displaying DOM elements based on a countdown timer. 我目前正在一个项目中,该项目基于倒数计时器显示和显示DOM元素。 There is another function calling this one every second. 还有另一个函数每秒调用一次。

Here is a code sample: 这是一个代码示例:

function eventsOnTimer() {

  let x = 1;

  const interval = setInterval(() => {

    if (x >= 0.0 && x < 30.0) {
      document.getElementById('thing1').style.display = 'block';
      document.getElementById('thing2').style.display = 'none';
    } 

    else if (x >= 30.0 && x < 60.0) {
      document.getElementById('thing1').style.display = 'none';
      document.getElementById('thing2').style.display = 'block';
    }

    x++;

  }, 1000);

}

I'm trying to increase performance, and I'm doing this by trying to reduce the number of DOM requests and looking at alternative ways to fire code based on the countdown timer. 我试图提高性能,并通过尝试减少DOM请求的数量并寻找基于倒数计时器触发代码的替代方法来做到这一点。

Something like 就像是

function eventsOnTimer(id1, id2, ms) {
  let toggle = false, thing1 = document.getElementById(id1), thing2 = document.getElementById(id2);

  const interval = setInterval(() => {
      if(toggle){
        thing1.style.display = 'block';
        thing2.style.display = 'none';
      } else{
        thing1.style.display = 'none';
        thing2.style.display = 'block';
      }
      toggle = !toggle;
  }, ms);
}

eventsOnTimer('thing1', 'thing2', 30000);

You can store all of nodes references before run your timer to dicrease DOM access time (getElementById). 您可以在运行计时器之前存储所有节点引用,以减少DOM访问时间(getElementById)。

After that, using className instead of style property will be faster. 之后,使用className代替style属性会更快。 You juste need declared an specific CSS rule per state. 您只需要为每个状态声明一个特定的CSS规则。

I propose to you an generic function to set automatically all of your nodes with the same CSS class name. 我向您建议一个通用函数,以自动设置所有具有相同CSS类名称的节点。

JS JS

var nodeArray = [];
var max_node = 2; 

function storeNodeRef() {
 for(var i =1; i <= max_node; i++) {
  nodeArray.push( document.getElementById("thing"+i)); // Your nodes are declared with ID thing"X". "X" is a numeric value, set "max_node"  with the latest "X" value.
 }
  eventsOnTimer();
}

function setNodeClass(nodeClassName) {
  var i = 0;
  while(i < max_node) {
    nodeArray[i++].className = nodeClassName;
  }

}

function eventsOnTimer() {
  let x = 1;
  const interval = setInterval(() => {
    if (x==30 || x == 60) { // declare here your different state, you can use multiple if/elseif or multiple switch case.
      setNodeClass('hide myClass'+x); // Param : new className
    } 
    x++;
  }, 1000);
}

 storeNodeRef();

CSS CSS

  .process > div, .hide {display:none;}
  #thing2.myClass30, #thing1.myClass60, .process > div.show {display:block; }

HTML EXAMPLE HTML示例

<div class="process">
    <div id="thing1" class="show" >Hello World 1</div>
    <div id="thing2">Hello World  2</div>
</div>

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

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