简体   繁体   中英

Javascript Variable outside of function?

1| <script type="text/javascript">
2|    function more() {
3|          $("#innerSub2").animate({ scrollTop: "+=240px" },1000);
4|          var count = 1;
5|          document.getElementById("dot" +count).style.color="#c7c9e9";
6|          var count = count + 1;
7|          document.getElementById("dot" +count).style.color="#6464c1";
8|        }
9|     </script>

Helo, I am pretty new to javascript. So sorry if this is a stupid question.
On the first click event the variable works as i need it to( line 5, count=1)(line 7, count=2)
But on second click event i need ( line 5, count=2)(line 7, count=3) but as you can see it resets to 1 with line 4.
So the question is, how can I declare | var count = 1; | outside of function more() so it won't reset my variable? or if there is any other way to do this..
Also if there is a way to stop variable count from exceeding 3, please share
Thank you

You can define a variable outside your function, so it wont be erased, this way the scope of your variable will be global, having a lot of global variables is considered not to be a good practice you can learn more about scope and javascript/jquery basics in this book http://jqfundamentals.com/book/index.html#example-2.44

<script type="text/javascript">
   var count = 1;
   function more() {
         $("#innerSub2").animate({ scrollTop: "+=240px" },1000);          
         document.getElementById("dot" +count).style.color="#c7c9e9";
         count = count + 1; //removed the var word.
         document.getElementById("dot" +count).style.color="#6464c1";
       }
</script>

My suggestion is to use const .

var info=[0,1];

function addView(btnIndex, btn){
    const index=btnIndex;
    btn.on("click", function(d){
        console.log(info[index]);
    };

}

Exactly how u wrote

  var count = 1;
  function more() {
          $("#innerSub2").animate({ scrollTop: "+=240px" },1000);
          document.getElementById("dot" +count).style.color="#c7c9e9";
          count = (count>2)?3:(count+1);
          document.getElementById("dot" +count).style.color="#6464c1";
   }
 var count = 1;
 function more(count) {
          $("#innerSub2").animate({ scrollTop: "+=240px" },1000);
          document.getElementById("dot" +count).style.color="#c7c9e9";

          if(count < 3)
              count += 1;

          document.getElementById("dot" +count).style.color="#6464c1";

          return count;
    }



   then you just call more(count) on some event.

Since the var is inside function, and you are setting it to = 1, it will always 'reset' to 1. What you need to do is:

var count = 1;
function more(){
    //js stuff animate
    count = count+1;
    //other js css
    count = count+1;
    //another js color
}

This is using your structure. You want to stop counting after 3 but still the other js to work or js stop working also? Lets make both as example. 3 but js still work:

var count = 1;
function more(){
    //js stuff animate
    if(count<3){count = count+1;}
    //other js css
    if(count<3){count = count+1;}
    //another js color
}

if you want all stop working then:

var count = 1;
function more(){
    if(count<3){
        //js stuff animate
        count = count+1;
        //other js css
        count = count+1;
        //another js color
    }
}

There are better form of presenting but i used your code structure so you could better understand.

If sometime you need to reset the count in the same page, since the var is outside rhe function, it is a global var so other functions can access / modify. For eg

function resetCount(){
    count = 1;
}

Also, to add to Syred's answer, you should put some error checking around the DOM object calls. For example:

<script type="text/javascript">
   var count = 1;
   function more() {
         $("#innerSub2").animate({ scrollTop: "+=240px" },1000);
         if (document.getElementById("dot" + count) != null) {
             document.getElementById("dot" +count).style.color="#c7c9e9";
             count = count + 1; //removed the var word. (could also use count++)
             if (document.getElementById("dot" + count) != null)
                 document.getElementById("dot" +count).style.color="#6464c1";
         }
   }
</script>

If I knew more about how you were using the function, I might make other recommendations on how to streamline and check for errors, but this should give you a good idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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