javascript - setTimeout keeps firing, how to make it fire only once? -


i making simple chat, works in different browsers settimeout keeps firing, , want fire once pointless keep firing , believe cause more stress on server.

this function called somewhere else:

function chat_load() {     $.post('chat.php', {stage:'load'}, function(data) {         $('#window').html(data);         settimeout("chat_load();", 1000);      }); } 

i tried following keeps on firing. also, function supposed fire when button clicked, happens once every often.

var c = 0; function chat_load() {     $.post('chat.php', {stage:'load'}, function(data) {         $('#window').html(data);         var t = settimeout("chat_load();", 1000);         c++;         if (c == 3) {             cleartimeout(t);         }     }); } 

i think want this:

var c = 0, t; function chat_load() {     $.post('chat.php', {stage:'load'}, function(data) {         $('#window').html(data);         cleartimeout(t);         t = settimeout(chat_load, 1000);                     if (++c === 3) {             cleartimeout(t);             c=0;         }     }); } 

Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -