Skip to content

Instantly share code, notes, and snippets.

@luke
Last active March 14, 2018 12:12
Show Gist options
  • Save luke/8d24ba17a0d7dc8f7dc14b703db2dfff to your computer and use it in GitHub Desktop.
Save luke/8d24ba17a0d7dc8f7dc14b703db2dfff to your computer and use it in GitHub Desktop.
function onTimezoneOffsetChange(changeHandler){
function getTimezoneOffset(){
const d = new Date()
return d.getTimezoneOffset()
}
function getNextHourOffset(){
const d = new Date()
return (1000*60*60) - (((d.getMinutes()*60) + d.getSeconds()) * 1000) + d.getMilliseconds()
}
let lastTZOffset = getTimezoneOffset()
let timeoutRef
function scheduleCheck(){
timeoutRef = setTimeout(function(){
const tzOffset = getTimezoneOffset()
if(lastTZOffset !== tzOffset){
lastTZOffset = tzOffset
changeHandler(tzOffset)
}
scheduleCheck()
}, getNextHourOffset()+1) // run 1ms after end of hour
}
scheduleCheck()
}
onTimezoneOffsetChange(function(tzOffset){
console.log("timezone changed")
// location.reload() or refresh somehow
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment