Ardi Vaba
Ardi Vaba

Reputation: 25

jQuery - Animate & Time interval

<script>
 $(document).ready(function() {

 $.get("database.php", function(data){
  var xp = data + "%";
  $('#exp_bg').animate({
   width: xp
  }, 1500, function() { });
 });

});
</script>

Database.php:

<?php
 $xp = 50;
 $level = 100;

 $xp_percent = $xp * 100 / $level;
 echo $xp_percent;
?>

What i'm trying to do, is that if $xp_percent increases in database.php, then #exp_bg's width will animate into desired width without refresh.

I tried to do that with time interval, which takes data from database.php every speciefed interval of time, but i failed doing that.

Could anyone help me with that?

Upvotes: 1

Views: 934

Answers (1)

Doug Molineux
Doug Molineux

Reputation: 12431

Your probably going to have to put it inside a setTimeout function like so:

var refreshId = setInterval(function() { 

$.get("database.php", function(data){
  var xp = data + "%";
  $('#exp_bg').animate({
   width: xp
  }, 1500, function() { });
 });


}, 1000);

This would check every second.

Upvotes: 1

Related Questions