最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to assign the value of 0 to a variable? - Stack Overflow

programmeradmin0浏览0评论

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');
  }
发布评论

评论列表(0)

  1. 暂无评论