0

I have tried to program a little self-updating iFrame that can be turned on and off. I have run into a few problems though. I managed to get the buttons working quite easily, and made them able to change my exe value which should be the trigger for the actual reloading process.

Now, this seemed easy in my head but I have tried the ideas that I had, and I am not too familiar with JavaScript. - The sleep function is taken elsewhere so that I can reload the page, sleep for x amount of milliseconds before it reloads again (while the start button is the active one).

The problem is to get the frame to actually update, and do it according to the exe value. I tried with a while statement, but even with my sleeper it just saw it as an infinite loop and froze, leaving me unable to control it with buttons. I tried making it a function with an if-statement to see if Javascript somehow kept calling the code, that wasn't the case... So now I am a bit confused on how to solve this problem and make the page actually reload every time.

<!DOCTYPE html>
<html>
<head>
  <title>Updater</title>
</head>

<body>
<script language="javascript"> 
var exe = 1;

function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}

function reload() {
document.getElementById('update_frame').contentWindow.location.reload(true);
sleep();
}


if (exe == 1) {
windows.setInterval(reload(), ms);
}



function start() {
exe = 1;
}

function stop() {
exe = 0;
}


</script>


<IFRAME name="update_frame" src="http://www.google.com" width="300" height="200" scrolling="no" frameborder="0" marginwidth="0" marginheight="0"></IFRAME>
<br />
<button onclick="start()">Start</button>  
<button onclick="stop()">Stop</button>  
</body>

</html> 

Tips to solving this puzzle would be appriciated! Thanks in advance :D

2 Answers 2

1

First change:

windows.setInterval(reload(), ms);

to:

windows.setInterval(reload, ms);

reload() calls a function and returns a result which is given as an argument to setInterval. And you do not want that because you want to give as an argument reference to a function, not its result.

Sign up to request clarification or add additional context in comments.

Comments

0

In order to properly stop, you'll need to store a handle to the interval to clear later:

var itvlReload;
function start() {
    itvlReload = setInterval(reload, ms);
}
function stop() {
    clearInterval(itvlRload);
}

This removes the need for the exe variable.

Alternatively, in your reload function, query exe:

function reload() {
    if (exe) {
        document.getElementById('update_frame').contentWindow.location.reload(true);
    }
}

Finally, you don't actually need sleep() (it's actually quite bad, since it uses up cycles even when "idle"), since you're using an interval.

3 Comments

I've tried both you and Michals ideas, however I can't seem to make it work - The problem seems to be with my refreshing code, am I doing something wrong there? The goal is for something similar to this: <meta http-equiv="refresh" content="5" > Just with a random timer and the ability to turn it on and off.
I can take another look later when I'm back at my computer, if you can stand to wait 5-6 hours :)
I got it working! But thanks for the offer! Oh and by the way, it was with your help I managed to get it working - I didn't realize exactly how the functions were to be used.

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.