Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you add 1 every second?

I have tried everything, I can't do it!!! Here's my code please help:

<!DOCTYPE html>
    <html>
         <head>
              <link rel="shortcut icon" href="favicon.ico"/>
              <title>Doughtnut Box !</title>
              <!--Design-->
              <style>

              </style>
              <!--jQuery and Javascript-->
              <script>
                 var doughnut = 0;
                   window.setInterval(
                        doughnut + 1
                   ,1000);
                   document.getElementById("doughnuts").innerHTML="You have " + doughnut >+ " doughnuts!";
              </script>
         </head>
         <body>
              <h3 id="doughnuts"></h3>
         </body></html>

Thanks for the code, it was really great! Now how can I save it so that it won't start over when I refresh?

like image 252
user2936538 Avatar asked Dec 06 '22 04:12

user2936538


1 Answers

Put them in a function. Also, change your expression to an assignment.

<script>
     var doughnut = 0;
     window.setInterval(
     function () {
         doughnut = doughnut + 1;
         document.getElementById("doughnuts").innerHTML = "You have " + doughnut + " doughnuts!";

     }, 1000);
</script>

Runnable demo below:

var doughnut = 0;
window.setInterval(function () {
  doughnut = doughnut + 1;
  document.getElementById("doughnuts").innerHTML = "You have " + doughnut + " doughnuts!";
}, 1000);
<div id="doughnuts"></div>
like image 137
acdcjunior Avatar answered Dec 08 '22 16:12

acdcjunior