-1

i have following piece of code which works fine but i want to stop the auto refreshing when certain condition is met .

enter code here
              $(document).ready(function() {


    setTimeout( function(){  $.ajax({
    url: '/jrt/?Id=$data.jacket.id',
    method: "GET",
    cache: false,
    success: function(data) {
        //$("#gt").append(data);
        $( '#gt' ).html( data );
    },
    error: function(jqXHR, textStatus, errorThrown) {
        alert('error ' + textStatus + " " + errorThrown);
    }
})
   },10000);


   });
DEO
  • 316
  • 2
  • 4
  • 18

2 Answers2

0
$(document).ready(function() {

var myVar;

function yourFunction() {
    myVar =  setTimeout( function(){  
        $.ajax({
            url: '/jrt/?Id=$data.jacket.id',
            method: "GET",
            cache: false,
            success: function(data) {
                $( '#gt' ).html( data );
            },
            error: function(jqXHR, textStatus, errorThrown) {
                alert('error ' + textStatus + " " + errorThrown);
            }
        })
    },10000);
}

function stopYourFunction() {
    clearTimeout(myVar);
} });

Use stop clearTimeout, to stop your setTimeOut function. Refer this w3schools, has a good explanation

Raja Sekar
  • 2,062
  • 16
  • 23
0

Assign setTimeout function in variable and clear it in your any condition.

var timer;

$(document).ready(function() {
  timer = window.setTimeout(function() {
    $.ajax({
      url: '/jrt/?Id=$data.jacket.id',
      method: "GET",
      cache: false,
      success: function(data) {
        //$("#gt").append(data);

        $('#gt').html(data);

        if (1) // your any condition
        {
          window.clearTimeout(timer);
        }
      },
      error: function(jqXHR, textStatus, errorThrown) {
        alert('error ' + textStatus + " " + errorThrown);
      }
    })
  }, 10000);
});
Dhruv
  • 173
  • 11