my function variable reset when I call function javascript

Erfan Ta

I making scroll animation progress bars, when the scroll reaches the element's width change from 0 to 78%. but there is one problem whenever I scroll again in the same height condition my width reset. I know why this happens. in fact, the range variable(i comment in JS) reassigns to 0 whenever I scroll, I don't know how to fix it. I read about Closures but couldn't understand how I must apply that to my code thanks for reading and for your time

//my js code
const SEORange = document.getElementById("SEO");


window.addEventListener("scroll", () => {
  let top = SEORange.getBoundingClientRect().top;
  let range = 0;  // the problem is here range will be reset 
  //whenever I scroll
  if (top < window.innerHeight * 0.905) {
    setInterval(() => {
      if (range == 78) {
        clearInterval;
      } else {
        range++;
        SEORange.style.width = range + "%";
      }
    }, 10);
  }
});
/*css file*/
*{
height:100vh  /*I add some height to see what happen if scroll*/

}
#section2-text-container {
  justify-content: end;
  flex-basis: 50%;
  margin-top: 5%;
}

#section2-text-container form {
  margin-top: 10%;
}
.range-container {
  background-color: #000000;
  border-radius: 0 10px 10px 0;
  width: 90%;
  height: 8px;
  position: relative;
}
#section2-text-container input {
  appearance: none;
  -webkit-appearance: none;
  background-color: #64bfd2;
  width: 0;
  height: 4px;
  margin: 0 5% 0 0;
  position: absolute;
  inset: 0 0 0 0;
}

#section2-text-container input::-webkit-slider-thumb {
  appearance: none;
  -webkit-appearance: none;
  width: 0px;
  background: #010101;
  height: 0px;
}
#section2-text-container input::-moz-range-thumb {
  width: 0px;
  background: #04aa6d;

}
 <!--html file-->
 
 
 <!DOCTYPE html>
 <html>
 <body> 
   <div id="section2-text-container">

       <form>
          <label for="SEO">SEO</label><br><br>
          <div class="range-container">
          <input id="SEO" disabled type="range" value="0" />
         </div>
        </form>
        
     </div>
</body>
</html>

c0m1t

First you have to make sure that you clear the interval, because 'scroll' event will be fired multiple times each second when the user scrolls.

const SEORange = document.getElementById("SEO");
let intervalId;
let range = 0;

window.addEventListener("scroll", () => {
  let top = SEORange.getBoundingClientRect().top;

  // If intervalId is set, it means the code has been ran once.
  if (top < window.innerHeight * 0.905 && !intervalId) {
    intervalId = setInterval(() => {
      if (range === 78) {
        clearInterval(intervalId);
      } else {
        range++;
        SEORange.style.width = range + "%";
      }
    }, 10);
  }
})

Another solution would be to add a class to trigger the transition you want. Which is IMO way better(smoother) than setting the width using an interval.

const SEORange2 = document.getElementById("SEO2");

window.addEventListener("scroll", () => {
  let top = SEORange.getBoundingClientRect().top;

  if (top < window.innerHeight * 0.905) {
    SEORange2.classList.add("animate");
  }
});
#SEO2 {
  height: 30px;
  width: 0;
  background-color: yellow;
  transition: width 780ms linear;
}

#SEO2.animate {
  width: 78%;
}

codesandbox

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How I can reset variable value when function end?

From Dev

Is there any way I should change my call to a Javascript function when I use bind?

From Dev

I downcasted my variable but I cannot call the function of the new class

From Dev

What is the Difference between function and function() When I call them in javascript?

From Dev

I want to call a function when my playing sound is finished in HTML and JAVASCRIPT

From Dev

Why do I need 'javascript:' in the call to my javascript function?

From Dev

Javascript variable function method call

From Dev

Call JSTL Variable to javascript function

From Dev

'this' is undefined when I call a function from my service

From Dev

Why won't my function activate when I call it?

From Dev

c free() works when I call but not in my function

From Dev

While Loop Doesn't End when i call my function

From Dev

Why is my python function not working properly when I call it recursively?

From Dev

Why does my program stop when I call a function?

From Dev

Use strict is not working when I call my hoisted function

From Dev

How do I call a Javascript function from my Android activity?

From Python

python: why it says undefined variable when I call function?

From Dev

(C++) Variable value changing when I call a function

From Dev

I cannot assign a value to a variable after call a function in javascript

From Dev

if (isset($_POST['UpdateBtn'])){ } to stored my input in variable and call the function but when I clink the button name(UpdateBtn) nothing happened

From Dev

Can I pass an argument into my decorator function when I call the inner/decorated function?

From Dev

When I call .addEventListener(click, myFunction(i), false); It runs my function on the call and not on the click

From Dev

Why I have an Invalid hook call when I call my function?

From Dev

When using function calls to template functions, I am faced with an unexpected matching template function for my call

From Dev

Why is a PUT request not sent by my function when I call another function to refresh the window?

From Dev

Why my RAM gets overloaded when I run javascript function?

From Dev

How can I call my feature file function in javascript Function of another feature file?

From Dev

How do I call the function when a variable and function have a same name?

From Dev

How do I call my function from within my function?

Related Related

  1. 1

    How I can reset variable value when function end?

  2. 2

    Is there any way I should change my call to a Javascript function when I use bind?

  3. 3

    I downcasted my variable but I cannot call the function of the new class

  4. 4

    What is the Difference between function and function() When I call them in javascript?

  5. 5

    I want to call a function when my playing sound is finished in HTML and JAVASCRIPT

  6. 6

    Why do I need 'javascript:' in the call to my javascript function?

  7. 7

    Javascript variable function method call

  8. 8

    Call JSTL Variable to javascript function

  9. 9

    'this' is undefined when I call a function from my service

  10. 10

    Why won't my function activate when I call it?

  11. 11

    c free() works when I call but not in my function

  12. 12

    While Loop Doesn't End when i call my function

  13. 13

    Why is my python function not working properly when I call it recursively?

  14. 14

    Why does my program stop when I call a function?

  15. 15

    Use strict is not working when I call my hoisted function

  16. 16

    How do I call a Javascript function from my Android activity?

  17. 17

    python: why it says undefined variable when I call function?

  18. 18

    (C++) Variable value changing when I call a function

  19. 19

    I cannot assign a value to a variable after call a function in javascript

  20. 20

    if (isset($_POST['UpdateBtn'])){ } to stored my input in variable and call the function but when I clink the button name(UpdateBtn) nothing happened

  21. 21

    Can I pass an argument into my decorator function when I call the inner/decorated function?

  22. 22

    When I call .addEventListener(click, myFunction(i), false); It runs my function on the call and not on the click

  23. 23

    Why I have an Invalid hook call when I call my function?

  24. 24

    When using function calls to template functions, I am faced with an unexpected matching template function for my call

  25. 25

    Why is a PUT request not sent by my function when I call another function to refresh the window?

  26. 26

    Why my RAM gets overloaded when I run javascript function?

  27. 27

    How can I call my feature file function in javascript Function of another feature file?

  28. 28

    How do I call the function when a variable and function have a same name?

  29. 29

    How do I call my function from within my function?

HotTag

Archive