Run script only once

Asked

Viewed 131 times

0

I set up a real-time alert script but wanted the alert to appear only once, it loops infinity even clicking close. How do I stop it after the code is executed or how to run only once?

This is my script that checks for new warnings (the big problem):

<script type="text/javascript">
    $(document).ready(function(e) {
        $.ajaxSetup({
            cache:true
        });
        setInterval(function() {
            $('#congelando').load('sy_avisos.php');
        }, 1500);
        $( "#congelando").click(function() {
            $('#congelando').load('sy_avisos.php');
        });
    });
</script>

This is my script to send the alert with $_POST:

if ($admin > 4)
   {  
$alertar = fopen("/sy_avisos.php", "w") or die("Ocorreu um erro ao enviar o 
alerta");
$txt = "scriptalert("'.$mensagem.'");script \n";
fwrite($alertar, $txt);
fclose($alertar);

I’m also taking suggestions to make it less complicated, but I’m out of ideas

  • Big problem is the setInterval.

  • Is there any way I can do this with Session or something? I’m impatient in this, no ideas, without the break I could not check if there are new warnings

  • Create a function for flame if you have new alert.

  • Why did you cancel my answer? Did you have a problem?

  • Sorry I pressed wrong I thought I had not validated

1 answer

1


Attribute the setInterval is a variable and cancel it in the callback of the .load():

$(document).ready(function(e) {
   $.ajaxSetup({
      cache:true
   });

   var timer = setInterval(function() {
      $('#congelando').load('sy_avisos.php', function(){ // callback
         clearInterval(timer); // cancela o setInterval
      });
   }, 1500);

   $( "#congelando").click(function() {
      $('#congelando').load('sy_avisos.php');
   });
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.