user6693981
user6693981

Reputation:

setInterval function is not working

I’m making a local time watch in jQuery. I wrote some piece of code but it’s not working. Here is my code:

$(document).ready(function(){
    function addZero(i) {
        if (i <= 9) {
            i = "0" + i;
        }
       return i;
    }
    var d = setInterval(function(){
        var z = new Date();
        var h = addZero(z.getHours());
        var m = addZero(z.getMinutes());
        var s = addZero(z.getSeconds();
        var a = '';
        if (h > 11 ) a = "PM" 
        else a = "AM"
        if (h == 16) h = '0'+4
        $('pre').html(h + ":" + m + ":" + s + "&nbsp;" + "a");
    },1000);
});

Upvotes: -1

Views: 73

Answers (3)

Vijay Barnwal
Vijay Barnwal

Reputation: 154

Please use below code in your code there is a function var d which is declared for setInterval but I didnt see this function as a called. so from where it will call. above answer is also fine and correct but global variable declaration is not needed here for d,z,h,m,s,a in below code. Local variable is also working.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
  //var d,z,h,m,s,a
  function addZero(i) {
    if (i <= 9) i = "0" + i;
    return i;
  }
  setInterval( function(){
    var z = new Date();
    var h = addZero(z.getHours());
    var m = addZero(z.getMinutes());
    var s = addZero(z.getSeconds());
    var a = '';
    if (h > 11 ) a = "PM" 
    else a = "AM"
    if (h == 16) h = '0'+4
    $('pre').html(h + ":" + m + ":" + s + "&nbsp;" + a);
  },1000);
});
</script>
<pre id="timer"></pre>

Upvotes: 0

Suren Srapyan
Suren Srapyan

Reputation: 68685

You have missed one )

$(document).ready(function(){
  
   function addZero(i) {
     if (i <= 9) {
        i = "0" + i;
     }
  
    return i;
   }
  
  
   var d = setInterval(function(){
   var z = new Date();
   var h = addZero(z.getHours());
   var m = addZero(z.getMinutes());
   var s = addZero(z.getSeconds());
     
   var a = '';
   if (h > 11 ) {
      a = "PM" ;
   }
   else {
      a = "AM";
   }
  
   if (h == 16) {
       h = '0'+4;
   }
    
   $('pre').html(h + ":" + m + ":" + s + "&nbsp;" + a);
  
   },1000);
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<pre></pre>

Upvotes: 1

Abhishek Pandey
Abhishek Pandey

Reputation: 13578

You made some mistakes - check code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
  var d,z,h,m,s,a
  function addZero(i) {
    if (i <= 9) i = "0" + i;
    return i;
  }
  d = setInterval( function(){
    z = new Date();
    h = addZero(z.getHours());
    m = addZero(z.getMinutes());
    s = addZero(z.getSeconds());
    a = '';
    if (h > 11 ) a = "PM" 
    else a = "AM"
    if (h == 16) h = '0'+4
    $('#timer').html(h + ":" + m + ":" + s + "&nbsp;" + a);
  },1000);
});
</script>
<pre id="timer"></pre>

Upvotes: 1

Related Questions