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 to setInterval. – MegaTom Commented Aug 5, 2016 at 18:48
Add a ment  | 

4 Answers 4

Reset to default 6

This 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