views:

49

answers:

3

I need to reload a block of javascript every amount of time.. say

<script type="text/javascript">
    var frame = some sort of code;
</script>

i need that block of any function to be reloaded every 15 seconds without reloading the page itself .. something like jQuery time out but i don't know how to apply it.. any idea?

A: 

You can use setTimeout('function()', 15000); - put this line of code at the end of the function() so that it calls itself again after 15000ms.

The other way is just to call setInterval('function()', 15000); and this will call your function() every 15000ms.

The difference between the first and the second one is that the first calls the function after specific milliseconds (only once, so you need to insert it in the function itself) and the second one just calls the function every n milliseconds.

o15a3d4l11s2
what if i want to reload script from src like <script type="text/javascript" src="${rootPath}scripts/jQuery/jquery.min.js"></script> how can this be reloaded
Mohamed Emad Hegab
+1  A: 
var frame;
setInterval(function() {
  frame = someSortOf.Code();
}, 15000);

That will execute the provided function every 15 seconds, setting your value. Note the var frame is declared outside the function, which gives it global scope and allows it to persist after your function executes.

You should not really "reload" a script. What you really want to do is simply run an already loaded script on a set interval.

Squeegy
what if i want to reload script from src like <script type="text/javascript" src="${rootPath}scripts/jQuery/jquery.min.js"></script> how can this be reloaded
Mohamed Emad Hegab
Why would you want to reload jQuery?
Squeegy
just as example i donot want to :D
Mohamed Emad Hegab
A: 
function foo() {
    // do something here

    if (needRepeat) {
        setTimeout(foo, 15000);
    }
}

setTimeout(foo, 15000);
how
what if i want to reload script from src like <script type="text/javascript" src="${rootPath}scripts/jQuery/jquery.min.js"></script> how can this be reloaded
Mohamed Emad Hegab
http://stackoverflow.com/questions/1671522/eval-or-load-a-remote-script-several-times
how
i did that <script type="text/javascript">//<!-- setInterval(function() {function scriptc(a,b){ var __d=document; var __h = __d.getElementsByTagName("head").item(0); var s = __d.createElement("script"); s.setAttribute("src", a); s.id = b; __h.appendChild(s);}}, 15000);scriptc("http://pagead2.googlesyndication.com/pagead/show_ads.js");// --></script> but it didn't work at all
Mohamed Emad Hegab
move scriptc out from anonymous function to global block
how