0

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.

3
  • What are you trying to achieve? Why would the function change? Commented Jun 6, 2014 at 10:46
  • I am trying to reload the script alone for every 30 seconds. Commented 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. Commented Feb 20, 2015 at 2:46

2 Answers 2

7

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>
Sign up to request clarification or add additional context in comments.

5 Comments

this is just a rough idea, it does not take into account what namespaces, variables and so on must be deleted/ taken care of since the OP did not provide ANY information...
does this script starts the task of the external file every 30 seconds again and again or it just remove and add the file.
@loyola You cannot remove script from browser memory, you can just overwrite it but still need to take care about any data/event previously bound. So basically, whatever you are trying to do, seems the wrong way. That's said, this answer question as asked
@Alex This script is working fine. You are awesome man... Keep helping other. Thank you.
@A.Wolff Actually I am try to sync two image slides. I tried my other ways but I was not able to fix that. The above script works properly for me. I know I am doing in crazy way but I need a solution for this.
0

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.