Skip to content

Instantly share code, notes, and snippets.

@natp0ng
Forked from luke/timezonechange.js
Created March 14, 2018 12:11
Show Gist options
  • Select an option

  • Save natp0ng/743148b4e3d076effb1b5962c405db25 to your computer and use it in GitHub Desktop.

Select an option

Save natp0ng/743148b4e3d076effb1b5962c405db25 to your computer and use it in GitHub Desktop.
function onTimezoneOffsetChange(changeHandler){
function getTimezoneOffset(){
const d = new Date()
return d.getTimezoneOffset()
}
function getHourOffset(){
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()
}, getHourOffset()+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