admin管理员组文章数量:1316840
What I want my function to achieve for my javascript function is for every second to either randomly subtract or add (a random number) to a number held in a div.
Here's what I have so far.
It doesn't work, it seems to append the number to the end of the div value (100), and also it doesn't take into account that I want it to either randomly add or subtract (it just adds at the moment)
setInterval(function(){
random = (Math.floor((Math.random()*15)+1));
currentnumber = document.getElementById('number');
document.getElementById('number').innerHTML = currentnumber + random;
}, 1000);
What I want my function to achieve for my javascript function is for every second to either randomly subtract or add (a random number) to a number held in a div.
Here's what I have so far.
It doesn't work, it seems to append the number to the end of the div value (100), and also it doesn't take into account that I want it to either randomly add or subtract (it just adds at the moment)
setInterval(function(){
random = (Math.floor((Math.random()*15)+1));
currentnumber = document.getElementById('number');
document.getElementById('number').innerHTML = currentnumber + random;
}, 1000);
Share
Improve this question
asked Jan 21, 2014 at 23:44
J. PodolskiJ. Podolski
2116 silver badges16 bronze badges
3
-
currentnumber
should probably be.innerHTML
and not the element itself. – bozdoz Commented Jan 21, 2014 at 23:46 - Maybe you could try currentnumber = parseInt(document.getElementById('number').innerHTML); ? (edit: yes innerHTML is what you're after, otherwise you're manipulating the DOM object) – Dan H Commented Jan 21, 2014 at 23:46
-
1
For the record, this question was a clear oversight of missing
innerHTML
when declaring the variable. – bozdoz Commented Jan 22, 2014 at 4:14
4 Answers
Reset to default 7parse the current value as an integer, and then do another math.random and use it to decide negative or positive. Lastly, you need to use the innerHTML of currentnumber, not the entire node. So something like this should work:
setInterval(function(){
random = (Math.floor((Math.random()*15)+1));
var plusOrMinus = Math.random() < 0.5 ? -1 : 1;
random = random * plusOrMinus;
currentnumber = document.getElementById('number');
document.getElementById('number').innerHTML = parseInt(currentnumber.innerHTML) + random;
}, 1000);
WORKING FIDDLE
.innerHTML
returns you a string which you'll need to parse into an integer to perform the addition or subtraction. Have a look at a number of methods listed in the following SO question
How do I convert a string into an integer in JavaScript?
currentnumber
is a DOM object, and you can't add that to a number.
var div = document.getElementById('number');
div.innerHTML = Number(div.innerHTML) + 3;
Notice you are getting the innerHTML
of the div
, converting that to a Number()
, adding your random number to it, and THEN setting your div.innerHTML
to your new value.
http://jsfiddle/9LqQG/1/
Maybe try something like this:
setInterval(function(){
random = (Math.floor((Math.random()*15)+1));
currentnumber = parseInt(document.getElementById('number').innerHTML);
document.getElementById('number').innerHTML = currentnumber + random;
}, 1000);
Your currentnumber
variable needs to get the innerHTML of the element, then parse the string into an integer.
jsFiddle
本文标签: randomJavascript to randomly addsubtract to a number every secondStack Overflow
版权声明:本文标题:random - Javascript to randomly addsubtract to a number every second - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742014612a2413536.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论