我有这样的问题:我想显示计数器,这将改变每一秒,所以我需要更改Ruby变量,我将在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这个varibale:
$.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
@Bjoernsen也许他希望它能够独立于客户端(因为您肯定知道JavaScript可以很容易地被黑客入侵),比如每秒钟都会有一种安全密钥changin。 – freakish
@Bjoernsen,请看最新的问题。 – MID