Monday, 15 February 2010

html - Stop javascript execution if content is not shown -



html - Stop javascript execution if content is not shown -

i have div contains javascript updates div each x seconds, when replace div's content context, want javascript stop because returns old content , overrides current content. how can create javascript stop under conditions ?

<li><%= link_to "run task", { :controller => "tasks_runner", :action => "task_form"}, {:remote => true} %></li> <li><%= link_to "monitor tasks", { :controller => "tasks_runner", :action => "monitor_tasks"}, {:remote => true} %></li> <div class="content-module-main" id='task_runner_content'> </div>

each link updates div's contents. problem is, seconds link updates div table content , script updates table (updates task_runner_content content), , when click on first link, after x seconds content overridden seconds link's content because of javascript.

the update script:

<script> $(function() { $(window).ready(function() { settimeout(function() { jquery.ajax({ url: "/tasks_runner/monitor_tasks", type: "get", datatype: "script" }); }, 3000); //in 3 seconds }); }); </script>

if you're using setinterval or settimeout update contents (and sounds are), simply clear timer in function changes context, so:

function update(){ $('somediv').html('some value'); } var _timer = setinterval(update, 1000); function replacecontext(){ $('somediv').html('new context'); clearinterval(_timer); }

javascript html ruby-on-rails

No comments:

Post a Comment