2

I am creating a countdown of '90s` using jquery.

But the countdown is repeating.

I tried:

<script>
        var checkStatet = function(){
  jQuery.ajax({
    url: 'check_diffex.php?od=deotd'
  }).done(function(data){
      var o = data.diffex;
    var time = jQuery('#rbtntime');
    var timer = setInterval(function() {
  time.html(o);
  if (o == 0) {
    $('#rbtntimep').hide();
    clearInterval(timer);

  }
  o--;
}, 1000)
  });

}

checkStatet();
setInterval(checkStatet, 1000);
        </script>

<p>You need to wait <span id="rbtntime">90</span> before you can proceed</p>

check_diffex.php:

<?php
header('Content-Type: application/json');
$e = "90";
// You would calculate a real value here
echo json_encode([
  'diffex' => $e
]);
?>

I am creating a countdown of '90s` using jquery.

But the countdown is repeating.

MKJ
  • 133
  • 1
  • 1
  • 11
  • Is your file called `check-diffex.php` or `check_diffex.php` ? What's the purpose of the $_POST variable `od` ? It's probably resetting because you set `$e` in `check-diffex.php` without doing anything with `od=deotd` Or it's resetting because it can't even find the right page.. Is it even resetting? Like going down 1 second then back to 90? Please be more clear with your question. – IsThisJavascript Oct 26 '17 at 10:25
  • Check console logs aswell. This question should be closed and rewritten in my opinion. – IsThisJavascript Oct 26 '17 at 10:26
  • @WillParky93 $e is calculated by `$_POST` for checking, I directly defined `$e` – MKJ Oct 26 '17 at 10:28
  • 3
    `setInterval(checkStatet, 1);` You are running your function every 1 ms. – Hedegare Oct 26 '17 at 10:29
  • All we see with $e is that it gets set to 90, then outputted. Please don't skip out on your code. All it's going to do is hinder the progress of a proper answer – IsThisJavascript Oct 26 '17 at 10:29
  • @Jackowski I did 1000, Now it works well till 88 then it shows 90 87, 90 86 ..... – MKJ Oct 26 '17 at 10:34
  • Remove the last line `setInterval(checkStatet, 1000)` – Abhilash Nayak Oct 26 '17 at 10:41

1 Answers1

2

Try this. Remove the last setInterval:

<script>
 var checkStatet = function(){
  jQuery.ajax({
    url: 'check_diffex.php?od=deotd'
  }).done(function(data){
      var o = data.diffex;
      var time = jQuery('#rbtntime');
      var timer = setInterval(function() {
         time.html(o);
         if (o == 0) {
            $('#rbtntimep').hide();
            clearInterval(timer);
         }
         o--;
      }, 1000)
   });
 }
 checkStatet();
</script>
Hedegare
  • 2,037
  • 1
  • 18
  • 22
  • Thank you, it is helping me. How can we convert this count down in time means, instead of displaying 90 it should display 1:30, 80 - 1:20, 60 - 1:00, 50 - 0:50 ? – MKJ Oct 26 '17 at 10:46
  • I can't upvote it due to my reputations... I will create a new question and paste the link here. – MKJ Oct 26 '17 at 10:52
  • Don't need to ask a new question, check this it will help you: https://stackoverflow.com/questions/3733227/javascript-seconds-to-minutes-and-seconds – Hedegare Oct 26 '17 at 10:54