I have such problem: I want to show counter, which will be changing every second, so I need to change Ruby variable, which I will be using in jQuery.
EDIT: I want to make counter, that will be the same for all users.
I have counter.json.erb file:
{
"count": <%= @counter%>
}
and action:
def counter
@counter //here I need to change variable
respond_to do |format|
format.jsonr do
render :json
end
end
end
and I’m taking this varibale using jQuery:
$.getJSON('http://127.0.0.1:3000/counter.json', function(data) {
alert(data.count);//alert just for testing
})
EDIT: Suggested code:
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);
and I get error:
Uncaught SyntaxError: Unexpected end of input application.js:1
Maybe someone can suggest something ? Maybe other solution ?
You do not need to actually “change the variable every second”. It’s much simpler than that:
Done. You can repeat this algorithm at any time and always get the current status of the timer.