Is it possible to reload only JavaScript or Jquery function alone or Is it possible to reload the external Javascript file for every 30 seconds instead of reloading the page.
-
What are you trying to achieve? Why would the function change?Quentin– Quentin2014-06-06 10:46:25 +00:00Commented Jun 6, 2014 at 10:46
-
I am trying to reload the script alone for every 30 seconds.loyola– loyola2014-06-06 10:49:13 +00:00Commented Jun 6, 2014 at 10:49
-
Quentin, a developer may want to do this while doing a bunch of quick code changes, to avoid having to reload/reset a big page. That's why I was looking for it.ChrisPhoenix– ChrisPhoenix2015-02-20 02:46:18 +00:00Commented Feb 20, 2015 at 2:46
2 Answers
EDIT: I've edited my answer since apparently still a few people see and use this. I've added a callback which triggers a reload after 30 seconds just when the getScript works. You might want to leave this out or add a .done() / .fail() callback (have a look here), also.
Additionally I've added a random query value to make sure the script, that gets loaded, does not get cached:
<script id="myscript" src="src/to/file-with-your-custom-scripts.js"></script>
<script>
$(function() {
var salt = Math.floor(Math.random() * 1000),
time;
function load_script() {
$('#myscript').remove();
$.getScript("src/to/file-with-your-custom-scripts.js?s=" + salt, function() {
$('script:last').attr('id', 'myscript');
salt = Math.floor(Math.random() * 1000);
time = setTimeout(function() {
load_script();
}, 30 * 1000);
});
}
load_script();
});
</script>
Old version:
<script id="myscript" src="src/to/file-with-your-custom-scripts.js"></script>
<script>
$(function() {
setInterval(function() {
$('#myscript').remove();
$.getScript("src/to/file-with-your-custom-scripts.js", function() {
$('script:last').attr('id', 'myscript');
});
}, 30000); // every 30 seconds
});
</script>
5 Comments
Also to load the live data in our webpage without reloading we can use PHP MVC called CodeIgniter with Jquery. Please see the below link to make it easier. Also go to official website and find many more interesting things.
http://chrissilich.com/blog/make-your-own-live-data-feed-with-php-codeigniter-and-javascript-jquery/