ajax - jQuery condition for stop setInterval polling -
i have following jquery code:
<script> $(document).ready(function() { setinterval(function() { $.get('async', function(data) { $('#datum').text(data); } ); }, 1000); // 1000 milliseconds = 1 second. }); </script>
in case present string result servlet called async using following html:
the string either contains word running, or completed. want stop polling in above if data variable contains "completed". have tried following code snipplet confused place it, or if correct:
if (data ==="completed") { (var = 1; < 99999; i++) {window.clearinterval(i);} }
i have tried
if ( $('#datum').text(data) ==="completed" ) { (var = 1; < 99999; i++) {window.clearinterval(i);} }
i have made multiple attempts, of break functionality. without attempting stop polling, above works fine poll , return status "running", except when reaches "completed" continues polling well.
you need store return value of setinterval
call (which id of interval) in order clear interval later on. example:
<script> $(document).ready(function() { var intervalid = setinterval(function() { $.get('async', function(data) { $('#datum').text(data); if(data === 'completed'){ clearinterval(intervalid); } } ); }, 1000); // 1000 milliseconds = 1 second. }); </script>
Comments
Post a Comment