I have created a page that grabs some data from a php page every second using a timer. However, I am pretty sure there is a way to make the code a lot shorter and neater, perhaps using a loop.
Here is the code
- <script type="text/javascript">
- $(document).ready(function() {
- var delay = 10 ;
- function countdown() {
- setTimeout(countdown, 1000) ;
- $('#countmesg').html("Ends in " + delay + " seconds.");
- delay --;
- if (delay == 10 ) {
- $.post('grab.php', {name: name}, function(data) {
- $('div#name-data').text(data);
- });
- }
- if (delay == 9 ) {
- $.post('grab.php', {name: name}, function(data) {
- $('div#name-data').text(data);
- });
- }
- if (delay == 8 ) {
- $.post('grab.php', {name: name}, function(data) {
- $('div#name-data').text(data);
- });
- }
- if (delay == 7 ) {
- $.post('grab.php', {name: name}, function(data) {
- $('div#name-data').text(data);
- });
- }
- if (delay == 6 ) {
- $.post('grab.php', {name: name}, function(data) {
- $('div#name-data').text(data);
- });
- }
- if (delay == 5 ) {
- $.post('grab.php', {name: name}, function(data) {
- $('div#name-data').text(data);
- });
- }
- if (delay == 4 ) {
- $.post('grab.php', {name: name}, function(data) {
- $('div#name-data').text(data);
- });
- }
- if (delay == 3 ) {
- $.post('grab.php', {name: name}, function(data) {
- $('div#name-data').text(data);
- });
- }
- if (delay == 2 ) {
- $.post('grab.php', {name: name}, function(data) {
- $('div#name-data').text(data);
- });
- }
- if (delay == 1 ) {
- $.post('grab.php', {name: name}, function(data) {
- $('div#name-data').text(data);
- });
- }
- if (delay < 0 ) {
- $('#countmesg').html("Auction ended.");
- delay = 0 ;
- }
- }
- countdown() ;
- });
- </script>