topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Friday April 19, 2024, 3:38 pm
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: [JS] How to make this loop?  (Read 2521 times)

RedPillow

  • Member
  • Joined in 2008
  • **
  • Posts: 141
  • Pillows.
    • View Profile
    • Read more about this member.
    • Donate to Member
[JS] How to make this loop?
« on: March 30, 2011, 08:29 AM »
Code: Javascript [Select]
  1. $(document).ready(function(){
  2.  
  3.    var modTime = 0;
  4. setInterval(function(){
  5.   $.post("ChatClient.php", {"file":"chat.cht", "time":modTime}, function(rst) {
  6.     if (rst.time != modTime) {
  7.       modTime = rst.time;
  8.                 window.location.reload();
  9.         else {
  10.                        
  11.         }
  12.     }
  13.   });
  14. }, 1000);
  15. });

So that is my code, and I need to make it do the whole check again on the else-statemen.
This is supposed to check a file modification time and if it's changed - it should reload the page and if it's not changed, it should just keep checking infinitely again until it is.
It works nice, if I remove the else-statement but then it refreshes every seconds - no matter if the file is modified or not.

I tried this by myself:

Code: Javascript [Select]
  1. $(document).ready(function(){
  2. loopIt();
  3. function loopIt() {
  4.    var modTime = 0;
  5. setInterval(function(){
  6.   $.post("ChatClient.php", {"file":"chat.cht", "time":modTime}, function(rst) {
  7.     if (rst.time != modTime) {
  8.       modTime = rst.time;
  9.                 window.location.reload();
  10.         else {
  11.                 loopIt();      
  12.         }
  13.     }
  14.   });
  15. }, 1000);
  16. }
  17. });
« Last Edit: March 30, 2011, 08:41 AM by RedPillow »