Skip to content

Commit

Permalink
Refreshing without using intervals
Browse files Browse the repository at this point in the history
  • Loading branch information
ProgressoRU committed Jun 18, 2015
1 parent 1800984 commit de28847
Showing 1 changed file with 18 additions and 20 deletions.
38 changes: 18 additions & 20 deletions todo.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ angular.module('todoApp', [])
vm.is_start = true;
vm.currentRoomId = vm.lastRoomId;
vm.newRooms.push({id: vm.currentRoomId, status: 0, players: 0, activePlayers: 0, refresh: 0});
vm.startRefreshing();
vm.searchNewRoom();
};

Expand All @@ -56,34 +55,33 @@ angular.module('todoApp', [])
vm.newRooms[i].refresh = 1;
}
}
// vm.startRefreshing(vm.currentRoomId);
vm.startRefreshing(vm.currentRoomId);
vm.currentRoomId++;
vm.newRooms.push({id: vm.currentRoomId, status: 0});
vm.searchNewRoom();
}
})
};

vm.startRefreshing = function () {
console.log('refresh');
$interval(function () {
for (i = 0; i < vm.newRooms.length; i++) {
if (vm.newRooms[i].refresh == 1) {
$http.post('./room_status.php', {room_id: vm.newRooms[i].id}).then(function (response) {
for (j = 0; j < vm.newRooms.length; j++) { //дерьмокод
if (vm.newRooms[j].id == response.data.id) {
vm.newRooms[j].players = response.data.players;
if (response.data.players > vm.maxPlayers) {
vm.newRooms[j].refresh = 0;
}
vm.newRooms[j].activePlayers = response.data.activePlayers;
vm.newRooms[j].status = response.data.status;
}
}
})
vm.startRefreshing = function (roomId) {
console.log('refreshing' + roomId);
$http.post('./room_status.php', {room_id: roomId}).then(function (response) {
for (j = 0; j < vm.newRooms.length; j++) { //дерьмокод
if (vm.newRooms[j].id == roomId) {
vm.newRooms[j].players = response.data.players;
vm.newRooms[j].activePlayers = response.data.activePlayers;
vm.newRooms[j].status = response.data.status;
if (response.data.players > vm.maxPlayers) {
vm.newRooms[j].refresh = 0;
}
else {
$timeout(function () {
vm.startRefreshing(roomId);
}, 5000);
}
}
}
}, 3000);
})
};

vm.scanRoomsStatus = function () {
Expand Down

0 comments on commit de28847

Please sign in to comment.