0

Is it possible to start or stop this interval?

    <script type="text/javascript">
    $(document).ready(function(){

    setInterval(function(){
        $('#content').load('file.php')
    }, 1000);

});
</script>

If I have some button that is inside file.php and it is clicked. Can I stop interval function from above inside this file?

Noob
  • 1
  • 5
  • Assume you have button with I'd="cancel-btn", so you can do it like this. $(document).ready(function(){ var interval= setInterval(function(){ $('#content').load('file.php') }, 1000); $("#cancel-btn").click(function(){ clearInterval(interval);}) }); – Ahmadbaba46 Apr 07 '16 at 11:46
  • I edited question, my button is inside file.php and let name file with interval fuc code is index.php I need to stop interval in index.php file from file.php – Noob Apr 07 '16 at 11:50
  • Then for example you set onclick="stopinterval()" on your file.php button , then edit your js like this: var interval; $(document).ready(function(){ interval= setInterval(function(){ $('#content').load('file.php') }, 1000); }); function stopinterval(){clearInterval(interval);} – Ahmadbaba46 Apr 07 '16 at 12:00
  • So this will stop interval on the other file ? – Noob Apr 07 '16 at 12:13
  • I though about something with ajax to send some data on click to index.php file where is code for interval and to set $('#content') as $('') and in php to get data from ajax from file.php and to data be if(isset($_POST['something'])){ $something = $_POST['something']; }else $something = "#content"; – Noob Apr 07 '16 at 12:17

0 Answers0