DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

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

RedPillow

  • Pillows.
  • Member
  • Joined in 2008
  • **
  • Posts: 141
[JS] How to make this loop?
« on: March 30, 2011, 01:29 PM »
Code: (javascript)
 $(document).ready(function(){

   var modTime = 0;
setInterval(function(){
  $.post("ChatClient.php", {"file":"chat.cht", "time":modTime}, function(rst) {
    if (rst.time != modTime) {
      modTime = rst.time;
		window.location.reload();
	else {
			
	}
    }
  });
}, 1000);
});

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)
 $(document).ready(function(){
loopIt();
function loopIt() {
   var modTime = 0;
setInterval(function(){
  $.post("ChatClient.php", {"file":"chat.cht", "time":modTime}, function(rst) {
    if (rst.time != modTime) {
      modTime = rst.time;
		window.location.reload();
	else {
		loopIt();	
	}
    }
  });
}, 1000);
}
});
« Last Edit: March 30, 2011, 01:41 PM by RedPillow »

Original address: https://www.donationcoder.com/forum/index.php?topic=26228.0