【发布时间】:2012-09-14 10:24:25
【问题描述】:
我有这样的问题:我想显示计数器,它每秒都会改变,所以我需要更改我将在 jQuery 中使用的 Ruby 变量。
编辑:我想制作计数器,这对所有用户都是一样的。
我有 counter.json.erb 文件:
{
"count": <%= @counter%>
}
和行动:
def counter
@counter //here I need to change variable
respond_to do |format|
format.jsonr do
render :json
end
end
end
我正在使用 jQuery 获取这个变量:
$.getJSON('http://127.0.0.1:3000/counter.json', function(data) {
alert(data.count);//alert just for testing
})
编辑:建议代码:
var counter = 0
$(document).ready(function(){
var myCounter = new flipCounter('counter', {value:counter});
$.getJSON('http://127.0.0.1:3000/counter.json', function(data) {
myCounter.setValue(data.count);
})
});
setInterval(function() {
$.getJSON('http://127.0.0.1:3000/counter.json', function(data) {
myCounter.setValue(data.count);
}, 1000);
我得到错误:
Uncaught SyntaxError: Unexpected end of input application.js:1
也许有人可以提出一些建议?也许其他解决方案?
【问题讨论】:
-
为什么你的服务器上的 Ruby 中需要这个计数器?使用 jQuery 在客户端执行此操作会更好吗?
-
@Bjoernsen 也许他希望它独立于客户端(您肯定知道 JavaScript 很容易被黑客入侵),例如每秒更改某种安全密钥。
-
@Bjoernsen,请查看更新后的问题。
-
@freakish,请看更新的问题,也许你会更好地理解它。
-
您是指访问计数器还是计时器?
标签: jquery ruby-on-rails json