admin管理员组文章数量:1289867
I want a function to happen 5 sec after onload. Then, that function should change the value of a div every second. Basically, the div has a number, whose value i want increased every second , until 100. Here's what I have e up with so far:
div {
display:inline;
}
<body onload="setTimeout(start,3000) ; setTimeout(end,3000) ">
<div id = number>
1
</div>
%
<script type="text/javascript">
var i = 1;
setInterval(
var a = (function start() {
i = i%100 + 1;
return i;
}); , 1000)
setInterval(
var earth = document.getElementById('you');
function end (){
earth.innerHTML = a;
}
)
</script>
</body>
I want a function to happen 5 sec after onload. Then, that function should change the value of a div every second. Basically, the div has a number, whose value i want increased every second , until 100. Here's what I have e up with so far:
div {
display:inline;
}
<body onload="setTimeout(start,3000) ; setTimeout(end,3000) ">
<div id = number>
1
</div>
%
<script type="text/javascript">
var i = 1;
setInterval(
var a = (function start() {
i = i%100 + 1;
return i;
}); , 1000)
setInterval(
var earth = document.getElementById('you');
function end (){
earth.innerHTML = a;
}
)
</script>
</body>
No luck so far. Pls tell me where i went wrong and correct it pls. A fiddle would be appreciated :D .
Share Improve this question edited Aug 5, 2016 at 19:29 TrampolineTales 8264 gold badges15 silver badges32 bronze badges asked Aug 5, 2016 at 18:31 The East WindThe East Wind 621 gold badge3 silver badges11 bronze badges 2- 2 Hi Pranav, Can you please put this in a fiddle and share us a workable link to check the functionality? – David R Commented Aug 5, 2016 at 18:33
-
your main problem is that you put
var
in your call tosetInterval
. – MegaTom Commented Aug 5, 2016 at 18:48
4 Answers
Reset to default 6This should do what you're asking for:
setTimeout(start, 5000);
var i = 1;
var num = document.getElementById('number');
function start() {
setInterval(increase, 1000);
}
function increase() {
if (i < 100) {
i++;
num.innerText = i;
}
}
<div id="number">1</div>
Here you go use setTimeout
for 5sec gap , after that use setInterval
var counter = 0;
var div = document.getElementById('spinner');
setTimeout(function(){
var st = setInterval(function(){
div.innerHTML = ++counter;
if (counter > 100){
clearInterval(st);
}
},1000)
},5000);
<div id ="spinner"></div>
You should not put onload
in your <body>
tag.
Instead, use it in your JavaScript with window.onload
Make it as a function
function myFunction() {
setTimeout(
function() {
//stuff
}, 5000);
}
Then execute it onload
onload="myFunction();"
本文标签: javascriptIncreasing count of number every secondStack Overflow
版权声明:本文标题:javascript - Increasing count of number every second? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741429575a2378275.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论