Reputation: 135
I am building an application using Rails 3.2.6 and I want to create functionality that enables a part of a page reload itself automatically after some time interval. This is what I have done:
I created a partial called _hottest_beep where the reloaded content should be
I called the partial in main page like so
<div class="update_beep">
<%=render :partial=>"hottest_beeps"%>
</div>
I created a main.js.erb file
$(".update_beep").html("<%= escape_javascript(render("hottest_beeps"))%>")
I added a link on the main page that when that link is clicked the partial reloads automatically
<span ><%=link_to'View',{:controller=>'users',:action=>'main'},:remote=>true,:class=>'auto_click'%>
</span>
<div class="update_beep">
<%=render :partial=>"hottest_beeps"%>
</div>
Then I created a JavaScript file called auto_click
which will automatically click it every 1sec
var fsecs = 1000;
function feedTimer1()
{
if (fsecs == 0)
{
$('.auto_click').click();
fsecs =1000;
self.status = fsecs
ftimerRunning = true
ftimerID = self.setTimeout("feedTimer1()")
}
else
{
self.status = fsecs
fsecs = fsecs - 1
ftimerRunning = true
ftimerID = self.setTimeout("feedTimer1()")
}
}
window.onload = feedTimer1();
But is seems like the click even doesnt work. When I load up the page the partial remains the same, but when I click the link my self it works.
Note: I also called the auto_click.js file in the application.html.erb file which should load it to the page
Upvotes: 2
Views: 415
Reputation: 135
Thank you all for helping. But After many hours of work i eventually solved it. The problem was that my jquery file had a bug. changing it solved the problem :)
Upvotes: 0
Reputation: 222168
I'm not sure if I understand your js, but this should work:
$(function() {
function tick() {
$(".auto_click").click();
setTimeout(tick, 1000);
}
tick();
};
On a sidenote, you shouldn't be doing a setTimeout from within the function, as you will end up piling requests. Let me know if you need code for this.
Edit: For that, you can use this:
$(function() {
window.auto_click_tick = function() {
$(".auto_click").click();
}
window.auto_click_tick();
};
and in your js.erb file, add this
setTimeout(window.auto_click_tick, 1000);
Upvotes: 1