unix - Javascript refresh function problem -


i have problem following javascript code. want run background check (check.php) process on unix server. execution time of php , response takes 1.2-1.5 seconds (did test in mozilla) i've come script below refresh , check what's going on process , change background of div accordingly. right now, after few refreshes hangs, because think goes loop , doesn't wait first function finish , piles up. ideas doing wrong? more simpler way it?

<script language="javascript" type="text/javascript">      function getval(param)     {         var strurl = param;         var req = new xmlhttprequest();         req.open("get", strurl, false); //third parameter set false here         req.send(null);         return req.responsetext;     }      function changedivimage()     {            var pid = getval("check.php")         var imgpath = new string();         imgpath = document.getelementbyid("status_body").style.backgroundimage;              if(pid == 2)             {                 document.getelementbyid("status_body").style.backgroundimage = "url(active.png)";             }             else             {                 document.getelementbyid("status_body").style.backgroundimage = "url(down.png)";             }         changetimer();     }      function changetimer()     {         setinterval( "changedivimage()", 5000 );     }     </script> 

setinterval keep calling funciton on , over, calling changetimer funciton again after changedivimage returns stack more , more unwanted functions. try this:

<script language="javascript" type="text/javascript">      function getval(param)     {         var strurl = param;         var req = new xmlhttprequest();         req.open("get", strurl, false); //third parameter set false here         req.send(null);         return req.responsetext;     }     //this function calls self automatically , again everytime      //but when finishes operations.  (function changedivimage() {        var pid = getval("check.php")     var imgpath = new string();     imgpath = document.getelementbyid("status_body").style.backgroundimage;          if(pid == 2)         {             document.getelementbyid("status_body").style.backgroundimage = "url(active.png)";         }         else         {             document.getelementbyid("status_body").style.backgroundimage = "url(down.png)";         }       settimeout(changedivimage, 5000); })(); 


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -