私はそのような問題を抱えています:毎秒変化するカウンターを表示したいので、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
多分誰かが何かを提案することができますか?多分他の解決策?