How can I break out of an endless loop by clicking a button?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Smokeho
    New Member
    • Apr 2011
    • 1

    How can I break out of an endless loop by clicking a button?

    I've been searching for an answer to this for a long time, but found absolutely nothing. Obviously some AJAX has to be used, possibly setting some session variables, but everything I've tried has failed. Does anyone have a clue on how to do this?

    My code is too large, but I'll give you a small example, in case you need one:

    Code:
    <?php
    {
       //first part of code
    }
    
    //endless loop, based on the first part, until an onclick method sends an ajax request?
    while (1)
    {
       ...
       if ($variable_is_set_by_button_press) break;
    }
    
    {
       //last part of code giving me some feedback about execution time and such
    }
    ?>
  • Aimee Bailey
    Recognized Expert New Member
    • Apr 2010
    • 197

    #2
    PHP is a server side language, where as JavaScript and AJAX is client side. In order to stop a php script loading, you would need to check each loop cycle if a variable was set to true. And this variable would have to be stored in a static resource (such as a flat text file or a MySQL record), as you can't alter a script that is running using AJAX.

    In order to use AJAX, you would need to alter this static variable, then crossing fingers it should do what you ask.

    Hope this helps.

    Aimee.

    Comment

    Working...