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

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -