i have jquery script, running ajax script every 2 second
javascript: function getvalue(){ $.ajax({ url: "http://localhost:3000/companies/#{@company.url}", type: "get", datatype: "json", success: function (json) { $('.blabla').html(json.clicks); $('.tratata').html(json.shows); } }); } javascript: $(document).ready(function(){ getvalue(); setinterval('getvalue()', 2000); });
and when trying move script in assets, , call him via javascript_include_tag
- starts work throughout application, not in view call him.
how fix?
upd
i put script in app/assets/javascripts/foobar.js, , call him in view this
= javascript_include_tag 'foobar'
when rails generates app/assets/javascripts/application.js automatically includes line:
//= require_tree .
which tells rails include every file in directory application.js, default called in head of page. line in foobar.js file:
$(document).ready(function(){ getvalue(); setinterval('getvalue()', 2000); });
tells javascript run code every time dom loaded. why code running on every page.
to prevent have multiple options (see this post ideas) might easiest element in dom on pages want run, this:
$(document).ready(function(){ if( $("#element-id-only-on-foobar-pages").is(":visable") ) { getvalue(); setinterval('getvalue()', 2000); } });