Browse Source

avoid multiple updates and empty messages

AR 11 months ago
parent
commit
ac9cf7a6b1
1 changed files with 26 additions and 7 deletions
  1. 26 7
      index.php

+ 26 - 7
index.php

@@ -209,7 +209,6 @@ if (!isset($_SESSION['name'])) {
209 209
 					loadLog();
210 210
 				},
211 211
 				error: function(request, status, error) {
212
-					$("#usermsg").val(clientmsg);
213 212
 				},
214 213
 			});
215 214
 			
@@ -233,11 +232,8 @@ if (!isset($_SESSION['name'])) {
233 232
 			return false;
234 233
 		});
235 234
 		
236
-		//If user submits the form
237
-		$("#submitmsg").click(function() {
238
-			var clientmsg = $("#usermsg").val();
239
-			$("#usermsg").val('');
240
-			$("#usermsg").focus();
235
+		function sendMessage(clientmsg)
236
+		{
241 237
 			$.ajax({
242 238
 			type: 'POST',
243 239
 				url: 'post.php',
@@ -248,9 +244,21 @@ if (!isset($_SESSION['name'])) {
248 244
 					loadLog();
249 245
 				},
250 246
 				error: function(request, status, error) {
251
-					$("#usermsg").val(clientmsg);
247
+					// try again
248
+					setTimeout(function(){sendMessage(msg);}, 500);
252 249
 				},
253 250
 			});
251
+			
252
+		}
253
+		
254
+		
255
+		//If user submits the form
256
+		$("#submitmsg").click(function() {
257
+			var clientmsg = $("#usermsg").val();
258
+			if(clientmsg == '') return false;
259
+			$("#usermsg").val('');
260
+			$("#usermsg").focus();
261
+			sendMessage(clientmsg);
254 262
 			return false;
255 263
 		});
256 264
 
@@ -297,8 +305,12 @@ if (!isset($_SESSION['name'])) {
297 305
 			});
298 306
 		}
299 307
 
308
+		var loading_log = false;
309
+		
300 310
 		//Load the file containing the chat log
301 311
 		function loadLog() {
312
+			if(loading_log) return;
313
+			loading_log = true;
302 314
 			var oldscrollHeight = $("#chatbox")[0].scrollHeight;
303 315
 			$.ajax({
304 316
 			type: 'POST',
@@ -308,6 +320,9 @@ if (!isset($_SESSION['name'])) {
308 320
 				//cache: false,
309 321
 				async: true,
310 322
 				success: function(data) {
323
+					
324
+					loading_log = false;
325
+					
311 326
 					last_message = data.last_message;
312 327
 					
313 328
 					if(data['reset'])
@@ -342,6 +357,10 @@ if (!isset($_SESSION['name'])) {
342 357
 						if (!document.hasFocus()) audio.play();
343 358
 					}
344 359
 				},
360
+				error: function(request, status, error) {
361
+					// perhaps next time...
362
+					loading_log = false;
363
+				}
345 364
 			});
346 365
 		}
347 366