php - Retry Ajax Request 5 Times with 1 Second Interval -
i have verify.php file check database entry , return 1 if exists or 0 if doesn't. database updated between 0 5 seconds. @ least first 5 seconds return might 0.
in page, want check verify.php's return ajax , retry 5 times 1 second interval between each checks. if @ time in 5 second check, received 1, exit checking loop , display success message.
i have written checks once , don't know how make check x amount of times:
<p id="status"></p> <script type="text/javascript"> var url = 'verify.php'; $('#status').text('please wait...'); $(document).ready(function(){ $.ajax({ type: 'post', url:url, success: function(msg){ if (msg == 1){ $('#status').text('success'); }else{ $('#status').text('failed'); } } }); }); </script>
any appreciated. thanks.
- turn ajax call function
- have variable tracks how many tries have
- call ajax function in own callback delay of 1 second if needed, is, haven't exhausted tries , status wrong.
code
var tries = 0; function getstatus() { $.ajax({ type: 'post', url: url, success: function(msg){ tries++; if (msg == 1){ $('#status').text('success'); } else { if (tries >= 5 ) { $('#status').text('failed'); } else { settimeout(getstatus, 1000); } } } }); }
Comments
Post a Comment