fix polling function

This commit is contained in:
Daniela Arcese 2017-06-30 10:49:49 -07:00
parent 9026702e7b
commit 1908ce084d
1 changed files with 22 additions and 16 deletions

View File

@ -55,7 +55,7 @@ $(document).ready(function() {
} else { } else {
file = event.target.files[0]; file = event.target.files[0];
} }
const expiration = 24 * 60 * 60 * 1000; //will eventually come from a field const expiration = 2 * 60 * 1000; //will eventually come from a field
const fileSender = new FileSender(file); const fileSender = new FileSender(file);
fileSender.on('progress', percentComplete => { fileSender.on('progress', percentComplete => {
@ -140,7 +140,7 @@ $(document).ready(function() {
btn.classList.add('delete-btn'); btn.classList.add('delete-btn');
link.innerHTML = file.url.trim() + `#${file.secretKey}`.trim(); link.innerHTML = file.url.trim() + `#${file.secretKey}`.trim();
file.creationDate = new Date(file.creationDate); file.creationDate = new Date(file.creationDate);
const future = new Date(); const future = new Date();
@ -152,30 +152,36 @@ $(document).ready(function() {
let hours = Math.floor(minutes / 60); let hours = Math.floor(minutes / 60);
let seconds = Math.floor(countdown / 1000 % 60); let seconds = Math.floor(countdown / 1000 % 60);
if (hours > 0) { poll();
expiry.innerHTML = hours + 'h';
window.setInterval(() => {
poll();
expiry.innerHTML = hours + 'h';
}, 3600000);
} else if (hours === 0) {
expiry.innerHTML = minutes + 'm' + seconds + 's';
window.setInterval(() => {
poll();
expiry.innerHTML = minutes + 'm' + seconds + 's';
}, 1000);
}
function poll() { function poll() {
countdown = future.getTime() - new Date().getTime(); countdown = future.getTime() - new Date().getTime();
minutes = Math.floor(countdown / 1000 / 60); minutes = Math.floor(countdown / 1000 / 60);
hours = Math.floor(minutes / 60); hours = Math.floor(minutes / 60);
seconds = Math.floor(countdown / 1000 % 60); seconds = Math.floor(countdown / 1000 % 60);
let t;
if (hours > 1) {
expiry.innerHTML = hours + 'h';
t = window.setTimeout(() => {
poll();
}, 3600000);
} else if (hours === 1) {
expiry.innerHTML = hours + 'h';
t = window.setTimeout(() => {
poll();
}, 60000);
} else if (hours === 0) {
expiry.innerHTML = minutes + 'm' + seconds + 's';
t = window.setTimeout(() => {
poll();
}, 1000);
}
//remove from list when expired //remove from list when expired
if (hours === 0 && minutes === 0 && seconds === 0) { if (countdown <= 0) {
localStorage.removeItem(file.fileId); localStorage.removeItem(file.fileId);
$(expiry).parents('tr').remove(); $(expiry).parents('tr').remove();
window.clearTimeout(t);
} }
} }