0

I'm trying to develop a site which makes multiple calculations in one loop, and it takes from 20 secs up to 50-60 secs, based on server capacity.

The current solution is, that I call this function in every 60 secs with ajax, but this is a waste in the code: if the calculation ends in 30 secs, there is 30 secs more "waiting time".

I would like to optimize this code and simply recall this function once it ends. No return value is required.

I've tried the following solution, but somehow it's not working: after the first run the program quits, not calling the function again

<?php

  function Calculation() {

    // code that reads the input from a file
    // do the calculation stuff here that takes 20-60 secs

    Calculation(); // this should recall the function
  }

  Calculation(); // call for the first time

?>
8
  • 3
    Welcome on SO ! What do you mean by "somehow it's not working" ?
    – André DS
    Nov 7, 2018 at 20:08
  • Well it runs one time, than quits, not recalling the function
    – sdda
    Nov 7, 2018 at 20:09
  • and what's not working, you haven't shown is any actual code
    – user10226920
    Nov 7, 2018 at 20:09
  • Can you post your actual Calculation() function please?...
    – War10ck
    Nov 7, 2018 at 20:10
  • 2
    AJAX, can wait for a response from the server and then do something with that, it doesn't have to wait X seconds.
    – user10226920
    Nov 7, 2018 at 20:11

1 Answer 1

0

Thank you for the inputs guys, I already found a solution:

So now I set the ajax refresh time to 30 sec

<?php

  function Calculation() {

    $starttime = time();

    // code that reads the input from a file
    // do the calculation stuff here that takes 20-60 secs

    $endtime = time();

    if (($endtime - $starttime) > 30) { Calculation(); } 

    /* so basicly if the script runs more than 30 secs, 
    it won't wait the next refresh, just recalling itself. 
    If the script runs, the ajax won't reload the page as 
    mentioned. Once the program runtime will under 30 secs, 
    the basic ajax reload comes back. */

  }

  Calculation(); // call for the first time

?>
1
  • It seems you want an infinite while(true) {Calculation();} loop Nov 7, 2018 at 22:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.