In js, it is judged that an element stays in the visual area for more than 2 seconds before executing the corresponding function, otherwise what if it is not executed?

the element in the scroll bar event stays in the visual area for more than 2 seconds before the corresponding function is executed, otherwise it will not be executed

Feb.28,2021

learn to resist shaking

//  
  //  
  function isInSight2(ele) {
      var visibleArea = document.documentElement.clientHeight;//
      var eleTop = ele.offsetTop;//
      var scrollTop = document.documentElement.scrollTop;// 
      return eleTop - scrollTop < visibleArea ? true : false
  }
  //  https://segmentfault.com/a/1190000010744417
  function isInSight(el) {
      const bound = el.getBoundingClientRect(); // 
      const clientHeight = window.innerHeight;// 
      //
      //const clientWidth=window.innerWeight;
      return bound.top <= clientHeight + 100;
  }

it is estimated that you need a sign and a timer for 2 seconds. Combined with the above elements in the visual area, the general logic is:

  1. element enters the visualization, triggers the action, sets the flag amount, and starts the timer (note that the timer needs to be stored in variables for later deletion)
  2. The
  3. element exits the visual area, triggers the action, detects whether the start timer variable is null, deletes the timer if not, sets the variable to null, if it is null, does not act (may have performed the action after 2 seconds previously), and sets the flag status
  4. timer is triggered after 2 seconds. Check the amount of flag set, set the timer variable to null, for corresponding processing, and delete the timer at the end
Menu