Created
September 11, 2017 20:57
-
-
Save aliaspooryorik/ed413c86885e0ac1694b69c48ee84927 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/gemakof
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script src="https://cdn.jsdelivr.net/momentjs/2.14.1/moment-with-locales.min.js"></script> | |
<script src="https://cdn.jsdelivr.net/momentjs/2.14.1/locales.min.js"></script> | |
<script id="jsbin-javascript"> | |
console.clear(); | |
(function timeout(storage, moment) { | |
var STORAGE_KEY = 'ABC123'; | |
var MAX_SESSION_TIME_MS = 10000; | |
var WARN_DURATION_MS = 5000; | |
var timer_warn = null; | |
// var timer_expired = null; | |
function checkStatus() { | |
clearTimeout(timer_warn); | |
var delayBeforeExpiry = delayUntilSessionExpires(); | |
if (delayBeforeExpiry === 0) { | |
notifyTimeout(); | |
} else { | |
var warnDelay = getWarnTimeDelay(); | |
if (warnDelay <= WARN_DURATION_MS) { | |
notifyWarning(); | |
} else { | |
console.log('CANCEL WARNING'); | |
} | |
var ms = calculateDelay(warnDelay, delayBeforeExpiry); | |
console.log('warnDelay: ' + warnDelay); | |
console.log('delayBeforeExpiry: ' + delayBeforeExpiry); | |
timer_warn = setTimeout(checkStatus, ms) | |
} | |
} | |
function calculateDelay(warnDelay, delayBeforeExpiry) { | |
if (warnDelay > 0 && delayBeforeExpiry > 0) { | |
return Math.min(warnDelay, delayBeforeExpiry); | |
} | |
return Math.max(warnDelay, delayBeforeExpiry); | |
} | |
function notifyWarning() { | |
console.warn("WARNING"); | |
var delay = delayUntilSessionExpires(); | |
} | |
function notifyTimeout() { | |
console.warn("TIMED OUT"); | |
} | |
function refreshSession() { | |
setRefreshedFromMoment(getNowMoment()); | |
} | |
function setRefreshedFromMoment(refreshed) { | |
console.log('SET ' + refreshed.toISOString()); | |
storage.setItem(STORAGE_KEY, refreshed.toISOString()); | |
} | |
function delayUntilSessionExpires() { | |
var x = getSessionExpiresMoment(); | |
var now = moment(); | |
var diff = 0 - moment().diff(x); | |
console.warn('delayUntilSessionExpires: ' + diff); | |
if (diff < 0) { | |
return 0; | |
} | |
return diff; | |
} | |
function getRefreshedMoment(refreshed) { | |
var isoString = storage.getItem(STORAGE_KEY, refreshed); | |
console.log('GET ' + isoString); | |
return moment(isoString); | |
} | |
function getNowMoment() { | |
return moment(); | |
} | |
function getSessionExpiresMoment() { | |
return getRefreshedMoment().add(MAX_SESSION_TIME_MS); | |
} | |
function getWarnTimeMoment() { | |
return getSessionExpiresMoment().subtract(WARN_DURATION_MS); | |
} | |
function getWarnTimeDelay() { | |
var warnTimeoutMoment = getWarnTimeMoment(); | |
var nowMoment = getNowMoment(); | |
var diff = getWarnTimeMoment().diff(getNowMoment()); | |
//console.log('warnTimeoutMoment: ' + warnTimeoutMoment.format('mm:ss')); | |
//console.log('nowMoment: ' + nowMoment.format('mm:ss')); | |
console.log('diff warn timeout: ' + diff); | |
if (diff > 0) { | |
return diff; | |
} | |
return 0; | |
} | |
// begin | |
(function() { | |
refreshSession(); | |
var ms = getWarnTimeDelay(); | |
checkStatus(); | |
}()); | |
})(localStorage, moment); | |
// run in another tab | |
(function timeout(storage, moment) { | |
var STORAGE_KEY = 'ABC123'; | |
var counter = 0; | |
function refreshSession() { | |
setRefreshedFromMoment(moment().add(20, 'seconds')); | |
if (counter < 2) { | |
counter++; | |
setTimeout(refreshSession, 4000); | |
} | |
} | |
function setRefreshedFromMoment(refreshed) { | |
// console.log('SET ' + refreshed.toISOString()); | |
storage.setItem(STORAGE_KEY, refreshed.toISOString()); | |
} | |
// begin | |
(function() { | |
refreshSession(); | |
}()); | |
})(localStorage, moment); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">console.clear(); | |
(function timeout(storage, moment) { | |
var STORAGE_KEY = 'ABC123'; | |
var MAX_SESSION_TIME_MS = 10000; | |
var WARN_DURATION_MS = 5000; | |
var timer_warn = null; | |
// var timer_expired = null; | |
function checkStatus() { | |
clearTimeout(timer_warn); | |
var delayBeforeExpiry = delayUntilSessionExpires(); | |
if (delayBeforeExpiry === 0) { | |
notifyTimeout(); | |
} else { | |
var warnDelay = getWarnTimeDelay(); | |
if (warnDelay <= WARN_DURATION_MS) { | |
notifyWarning(); | |
} else { | |
console.log('CANCEL WARNING'); | |
} | |
var ms = calculateDelay(warnDelay, delayBeforeExpiry); | |
console.log('warnDelay: ' + warnDelay); | |
console.log('delayBeforeExpiry: ' + delayBeforeExpiry); | |
timer_warn = setTimeout(checkStatus, ms) | |
} | |
} | |
function calculateDelay(warnDelay, delayBeforeExpiry) { | |
if (warnDelay > 0 && delayBeforeExpiry > 0) { | |
return Math.min(warnDelay, delayBeforeExpiry); | |
} | |
return Math.max(warnDelay, delayBeforeExpiry); | |
} | |
function notifyWarning() { | |
console.warn("WARNING"); | |
var delay = delayUntilSessionExpires(); | |
} | |
function notifyTimeout() { | |
console.warn("TIMED OUT"); | |
} | |
function refreshSession() { | |
setRefreshedFromMoment(getNowMoment()); | |
} | |
function setRefreshedFromMoment(refreshed) { | |
console.log('SET ' + refreshed.toISOString()); | |
storage.setItem(STORAGE_KEY, refreshed.toISOString()); | |
} | |
function delayUntilSessionExpires() { | |
var x = getSessionExpiresMoment(); | |
var now = moment(); | |
var diff = 0 - moment().diff(x); | |
console.warn('delayUntilSessionExpires: ' + diff); | |
if (diff < 0) { | |
return 0; | |
} | |
return diff; | |
} | |
function getRefreshedMoment(refreshed) { | |
var isoString = storage.getItem(STORAGE_KEY, refreshed); | |
console.log('GET ' + isoString); | |
return moment(isoString); | |
} | |
function getNowMoment() { | |
return moment(); | |
} | |
function getSessionExpiresMoment() { | |
return getRefreshedMoment().add(MAX_SESSION_TIME_MS); | |
} | |
function getWarnTimeMoment() { | |
return getSessionExpiresMoment().subtract(WARN_DURATION_MS); | |
} | |
function getWarnTimeDelay() { | |
var warnTimeoutMoment = getWarnTimeMoment(); | |
var nowMoment = getNowMoment(); | |
var diff = getWarnTimeMoment().diff(getNowMoment()); | |
//console.log('warnTimeoutMoment: ' + warnTimeoutMoment.format('mm:ss')); | |
//console.log('nowMoment: ' + nowMoment.format('mm:ss')); | |
console.log('diff warn timeout: ' + diff); | |
if (diff > 0) { | |
return diff; | |
} | |
return 0; | |
} | |
// begin | |
(function() { | |
refreshSession(); | |
var ms = getWarnTimeDelay(); | |
checkStatus(); | |
}()); | |
})(localStorage, moment); | |
// run in another tab | |
(function timeout(storage, moment) { | |
var STORAGE_KEY = 'ABC123'; | |
var counter = 0; | |
function refreshSession() { | |
setRefreshedFromMoment(moment().add(20, 'seconds')); | |
if (counter < 2) { | |
counter++; | |
setTimeout(refreshSession, 4000); | |
} | |
} | |
function setRefreshedFromMoment(refreshed) { | |
// console.log('SET ' + refreshed.toISOString()); | |
storage.setItem(STORAGE_KEY, refreshed.toISOString()); | |
} | |
// begin | |
(function() { | |
refreshSession(); | |
}()); | |
})(localStorage, moment); | |
</script></body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.clear(); | |
(function timeout(storage, moment) { | |
var STORAGE_KEY = 'ABC123'; | |
var MAX_SESSION_TIME_MS = 10000; | |
var WARN_DURATION_MS = 5000; | |
var timer_warn = null; | |
// var timer_expired = null; | |
function checkStatus() { | |
clearTimeout(timer_warn); | |
var delayBeforeExpiry = delayUntilSessionExpires(); | |
if (delayBeforeExpiry === 0) { | |
notifyTimeout(); | |
} else { | |
var warnDelay = getWarnTimeDelay(); | |
if (warnDelay <= WARN_DURATION_MS) { | |
notifyWarning(); | |
} else { | |
console.log('CANCEL WARNING'); | |
} | |
var ms = calculateDelay(warnDelay, delayBeforeExpiry); | |
console.log('warnDelay: ' + warnDelay); | |
console.log('delayBeforeExpiry: ' + delayBeforeExpiry); | |
timer_warn = setTimeout(checkStatus, ms) | |
} | |
} | |
function calculateDelay(warnDelay, delayBeforeExpiry) { | |
if (warnDelay > 0 && delayBeforeExpiry > 0) { | |
return Math.min(warnDelay, delayBeforeExpiry); | |
} | |
return Math.max(warnDelay, delayBeforeExpiry); | |
} | |
function notifyWarning() { | |
console.warn("WARNING"); | |
var delay = delayUntilSessionExpires(); | |
} | |
function notifyTimeout() { | |
console.warn("TIMED OUT"); | |
} | |
function refreshSession() { | |
setRefreshedFromMoment(getNowMoment()); | |
} | |
function setRefreshedFromMoment(refreshed) { | |
console.log('SET ' + refreshed.toISOString()); | |
storage.setItem(STORAGE_KEY, refreshed.toISOString()); | |
} | |
function delayUntilSessionExpires() { | |
var x = getSessionExpiresMoment(); | |
var now = moment(); | |
var diff = 0 - moment().diff(x); | |
console.warn('delayUntilSessionExpires: ' + diff); | |
if (diff < 0) { | |
return 0; | |
} | |
return diff; | |
} | |
function getRefreshedMoment(refreshed) { | |
var isoString = storage.getItem(STORAGE_KEY, refreshed); | |
console.log('GET ' + isoString); | |
return moment(isoString); | |
} | |
function getNowMoment() { | |
return moment(); | |
} | |
function getSessionExpiresMoment() { | |
return getRefreshedMoment().add(MAX_SESSION_TIME_MS); | |
} | |
function getWarnTimeMoment() { | |
return getSessionExpiresMoment().subtract(WARN_DURATION_MS); | |
} | |
function getWarnTimeDelay() { | |
var warnTimeoutMoment = getWarnTimeMoment(); | |
var nowMoment = getNowMoment(); | |
var diff = getWarnTimeMoment().diff(getNowMoment()); | |
//console.log('warnTimeoutMoment: ' + warnTimeoutMoment.format('mm:ss')); | |
//console.log('nowMoment: ' + nowMoment.format('mm:ss')); | |
console.log('diff warn timeout: ' + diff); | |
if (diff > 0) { | |
return diff; | |
} | |
return 0; | |
} | |
// begin | |
(function() { | |
refreshSession(); | |
var ms = getWarnTimeDelay(); | |
checkStatus(); | |
}()); | |
})(localStorage, moment); | |
// run in another tab | |
(function timeout(storage, moment) { | |
var STORAGE_KEY = 'ABC123'; | |
var counter = 0; | |
function refreshSession() { | |
setRefreshedFromMoment(moment().add(20, 'seconds')); | |
if (counter < 2) { | |
counter++; | |
setTimeout(refreshSession, 4000); | |
} | |
} | |
function setRefreshedFromMoment(refreshed) { | |
// console.log('SET ' + refreshed.toISOString()); | |
storage.setItem(STORAGE_KEY, refreshed.toISOString()); | |
} | |
// begin | |
(function() { | |
refreshSession(); | |
}()); | |
})(localStorage, moment); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment