Browse Source

don't spam xhr's when there is an error or user stops

raylu 14 years ago
parent
commit
dbf2f53eb6
3 changed files with 17 additions and 5 deletions
  1. 15 1
      static/script.js
  2. 1 3
      templates/p.html
  3. 1 1
      updates.go

+ 15 - 1
static/script.js

@@ -1,3 +1,5 @@
+var current_id = '';
+var ids = [];
 var player;
 var litColor = '#eee';
 var dimmedColor = '#555';
@@ -28,7 +30,18 @@ function getUpdates() {
 		dataType: 'json',
 		data: {pid: pid, timestamp: timestamp},
 		success: handleUpdates,
-		complete: getUpdates
+		error: function (_, textStatus) {
+			switch (textStatus) {
+			case 'abort':
+				return;
+			case 'error':
+			case 'parsererror':
+				setTimeout(getUpdates, 1000);
+				return;
+			default:
+				getUpdates();
+			}
+		}
 	});
 }
 
@@ -55,6 +68,7 @@ function handleUpdates(data) {
 			break;
 		}
 	}
+	getUpdates();
 }
 
 function drawBar(id) {

+ 1 - 3
templates/p.html

@@ -29,10 +29,8 @@
 	{{$first := "82KEVKGRAhI"}}
 	<script>
 		var pid = '{{.Id}}';
-		var current_id = '';
-		var ids = [];
 	</script>
-				<script src="/static/jquery.min.js"></script>
+	<script src="/static/jquery.min.js"></script>
 	<!--
 	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
 	-->

+ 1 - 1
updates.go

@@ -58,7 +58,7 @@ func addUpdate(pid int, action uint, song *Song) {
 	listeners[pid] = nil
 }
 
-// assumes locked
+// assumes caller has updateLock
 func checkUpdates(pid int, timestamp int64) *Update {
 	pup, _ := headUpdates[pid]
 	for pup != nil {