-3

I have like...

<div id="div"></div>
<script src="jquery.js"></script>
<script src="updater.js"></script>

The updater I want to get data from my .php file, so probably something like...

setInterval(function() {
    $.ajax({
        type: "GET",
        url: "test.php",
        success: function(html) {
             // html is a string of all output of the server script.
            $("#div").html(html);
       }
    });
}, 5000);

Then, obviously enough the test.php's page will result.

But, I want to STOP the setInterval(); WHEN someone clicks on a comment button (textarea). I'm aware, there's some on here; but they're not working?

GG.
  • 21,083
  • 14
  • 84
  • 130
AwesomeE
  • 11
  • 1
  • 1
  • 1
  • 5
    [`Window.clearInterval`](https://developer.mozilla.org/en-US/docs/Web/API/Window.clearInterval) mate – Satpal Aug 11 '14 at 12:29
  • P.S. That's not my actual code; just some random junk. But, that's the kind of format I have. – AwesomeE Aug 11 '14 at 12:29
  • For a full demo on how to stop an interval see the the JavaScript MDN docs on `setInterVal`, specifically [**Example 2**](https://developer.mozilla.org/en/docs/Web/API/window.setInterval#Example_2.3A_Alternating_two_colors) - `The following example will continue to call the flashtext() function once a second, until you clear the intervalID by clicking the Stop button.` - Relevant Code is in the stop button click. - Hope this helps :) – Nope Aug 11 '14 at 12:33
  • @AwesomeE: I'm not sure why you feel your questions is not answered. Can you tell us what else you are looking for please that `clearInterval` does not do? The code you need, as far as we can tell, is in the linked "possible" duplicate(s) but also in every answer, unless there are further requirements in addition to what you stated. `clearInterval(intervalId)` will stop the interval - How you invoke it is up to you. If you want simply a complete copy-paste solution, see [**this**](http://whathaveyoutried.com) – Nope Aug 11 '14 at 12:58

3 Answers3

5

Window.clearInterval, It accepts an intervalID is the identifier of the repeated action you want to cancel. This ID is returned from setInterval().

Example

var interVal = setInterval(function() {
    $.ajax({
        type: "GET",
        url: "test.php",
        success: function(html) {
             // html is a string of all output of the server script.
            $("#div").html(html);
       }
    });
}, 5000);

clearInterval(interVal); //Call the method when you want to clear it.
Satpal
  • 132,252
  • 13
  • 159
  • 168
3

The setInterval function will return an integer which identifies that interval. Save that integer for later use, and when you want to clear the interval pass it into clearInterval as an argument.

E.g.:

var myInterval = setInterval(function() {
    $.ajax({
        type: "GET",
        url: "test.php",
        success: function(html) {
             // html is a string of all output of the server script.
            $("#div").html(html);
       }
    });
}, 5000);

And then when you want to cancel it:

clearInterval(myInterval);

Reference: http://www.w3schools.com/jsref/met_win_clearinterval.asp

Colin P. Hill
  • 422
  • 4
  • 18
2

You can use clearInterval. It takes the Id of interval & clears it.

Get your setInterval in a variable.

var interval = setInterval(function(){...},time);

After clicking on button just do

clearInterval(interval);

DEMO

Mritunjay
  • 25,338
  • 7
  • 55
  • 68