admin管理员组

文章数量:1414621

Javascript function 1:

var count = 0;

function myFunction()
{
count++;
document.getElementById("count").innerHTML=count;
}

Javascript function 2:

function demo() {
var y=document.getElementById("count").innerHTML;
if(y=="0") {
alert("There's nothing to be reset.");
}
else {
var count=0;
alert("Reset.");
// alternative code I used: document.getElementById("count").innerHTML="0";
}
}

HTML code:

<a href="Javascript:myFunction()">Click here</a>
<p>Total:<span id="count">0</span></p>
<button onclick="demo()">reset</button>

Is there a way to reset the variable to 0 in this code?

I've tried to reset the variable count to zero using document.getElementById() and adding =0; to the variable. Neither of these work. For example, if the user was to click the link the count would increase to 1. When they click the reset button it would appear to reset to 0 (unless using `var count=0;). However, if the user were to click the link again the count would be return the value 2, as it could simply continue to increment from the previous time.

I'm sorry if this has already been answered somewhere else, but it's very difficult to search for the terms = and ++.

Javascript function 1:

var count = 0;

function myFunction()
{
count++;
document.getElementById("count").innerHTML=count;
}

Javascript function 2:

function demo() {
var y=document.getElementById("count").innerHTML;
if(y=="0") {
alert("There's nothing to be reset.");
}
else {
var count=0;
alert("Reset.");
// alternative code I used: document.getElementById("count").innerHTML="0";
}
}

HTML code:

<a href="Javascript:myFunction()">Click here</a>
<p>Total:<span id="count">0</span></p>
<button onclick="demo()">reset</button>

Is there a way to reset the variable to 0 in this code?

I've tried to reset the variable count to zero using document.getElementById() and adding =0; to the variable. Neither of these work. For example, if the user was to click the link the count would increase to 1. When they click the reset button it would appear to reset to 0 (unless using `var count=0;). However, if the user were to click the link again the count would be return the value 2, as it could simply continue to increment from the previous time.

I'm sorry if this has already been answered somewhere else, but it's very difficult to search for the terms = and ++.

Share Improve this question edited Feb 21, 2014 at 22:47 BenMorel 36.7k52 gold badges206 silver badges337 bronze badges asked Mar 26, 2013 at 19:19 user2166577user2166577 0
Add a ment  | 

3 Answers 3

Reset to default 2

You made it a local variable by using var

else {
    var count=0;
    alert("Reset.");

should be

else {
    count=0;
    alert("Reset.");

Two things:

  1. Get rid of var where you reset the variable. That's giving you a separate local variable with the same name.

  2. Using "count" as a variable name, if it's global, will cause problems if you've also got an element whose id is "count" in the DOM. Use a different variable name or a different id, or make sure that the variable is not global (can't tell from posted code).

The things you need to chnage in the code are:

And make the parison to

   if(parseInt(y) === 0) // You are converting it to integer and doing a strict check

the else construct to

  else {
    count = 0;
    alert('Reset');
  }

本文标签: javascriptHow to assign the value of 0 to a variableStack Overflow