【发布时间】:2015-04-10 15:52:06
【问题描述】:
我有一个http://thisbythem.com/blog/clientside-timezone-detection/ 的实现。以下是关键文件/代码:
app/controllers/application_controller.rb
around_filter :set_timezone
...
private
def set_timezone
if(current_learner)
timezone = Time.zone = current_learner.time_zone
else
timezone = Time.find_zone(cookies[:timezone])
end
Time.use_zone(timezone) { yield }
end
app/views/layouts/application.html.erb
<script class="code" type="text/javascript">
$(function() {
$('#flash_notification').hide().delay(500).slideDown('slow', function() {
$(this).delay(4000).fadeOut();
});
$("#flash_notification").live('click', function(){
$(this).stop().fadeOut();;
});
});
//set event times for non-auth users
var tz = jstz.determine();
$.cookie('timezone', tz.name(), { path: '/' });
</script>
除了上面添加的 jquery.cookie.js 和 jstz.main.js 到 app/assets/js。首次访问该站点时时区 cookie 设置正确,但是如果我更改系统时区并刷新,我看不到 cookie 中的更改。此外,如果我进入 js 控制台并执行以下操作:
tz=jstz.determine();
tz.name();
...我在这里也没有看到更新的时区设置。同时,如果我删除 cookie 并刷新,我会得到新的时区。
【问题讨论】:
-
能不能把js代码设置cookie
var tz = jstz.determine(); $.cookie('timezone', tz.name(), { path: '/' });移到$(function() {里面