0

i've a problem stopping a JS chrono job..My code is:

var timerID = setInterval(function() {


    $.ajax({
        url: urltocall,
        data: parameters,
        dataType: "json",
        success: function (data) {
            if (data=='go' && checkShow =='0') {
                        DO SOME ACTIONS
                }
        },
        error: function(){
            console.log('Error on timeSession.js ajax call');
        }
        });
},  interval * 1000);

This code is included in my header.php so it's executed very time the site is open :)

Add: as i wrote this JS file is included in my header.php so it's included in every page in my site. When the function get the GO value from the ajax call the site is redirected to another page (thanks a location.href = XXXX;) but because of the header.php the setinterval restart after the new page is loaded.

how can i block after changing page?

  • which is the duplicate question? – Steve Auges Jun 10 '16 at 08:27
  • The one linked to in the box at the top – Rory McCrossan Jun 10 '16 at 08:28
  • my question is different because in that question the stop interval function is intrnal in interval code. in my question i need to have an external stop function, out of the interval code – Steve Auges Jun 10 '16 at 08:29
  • add: as i wrote this JS file is included in my header.php so it's included in every page in my site. When the function get the GO value the site is redirected to another page (because of the header.php) and the setinterval restart. how can i block after changing page? – Steve Auges Jun 10 '16 at 08:34
  • 1) the answer is the same regardless of logic flow - `clearInterval()`. 2) You cannot retain the state of a timer through page requests. If you need that behaviour you would need to use either a single page site or frames, with the former being by far the better option – Rory McCrossan Jun 10 '16 at 09:43

0 Answers0