jQuery condition for stop setInterval polling
I have the following jQuery code:
<script>
$(document).ready(function() {
setInterval(function() {
$.get('async', function(data) {
$('#datum').text(data);
} );
}, 1000); // 1000 milliseconds = 1 second.
});
</script>
In which case I present back a String result from a servlet called async
using the following html:
The String either contains the word running, or completed. I want to stop
the polling in the above if the data variable contains "completed". I have
tried the following code snipplet but I am confused exactly where to place
it, or if it is even correct:
if (data ==="completed") {
for (var i = 1; i < 99999; i++) {window.clearInterval(i);}
}
I have also tried
if ( $('#datum').text(data) ==="completed" ) {
for (var i = 1; i < 99999; i++) {window.clearInterval(i);}
}
I have made multiple attempts, all of which break the functionality.
Without attempting to stop polling, the above works perfectly fine to
constantly poll and return the status "running", except when it reaches
"completed" it continues polling as well.
No comments:
Post a Comment