0

My slideshow is running continuously, without user interaction. Since the show runs fullscreen there are no controls, as meant. However, Occasionnaly the user should be able to freeze the show in case she want to inspect a picture [work of art] more carefully. I wonder if this can be done by adding an eventListener(keydown) enabling the user to toggle, say spacebar (Keycode == 32) to freeze or to continue the show. Relevant piece of code:

<body>
...

(function loop() {
  rantime = Math.round(Math.random() * (9000 - 2000)) + 1000;
  setTimeout(function() {
        rotateImage();
        loop();
  }, rantime);
})(); 

So far I didn't found any suggestion to solve this problem. [clearTimeout, if possible, only skips the present image for the next] The point of toggling, of course is that the user can postpone continuation of the show indefinitely, rather than finish the show. Any help would be appreciated. Wimsch

2
  • It seems you have tried to use clearTimeout (which is the correct solution) already? Can you show us that code? Commented Dec 16, 2014 at 4:47
  • No I can't. In fact this is an other question, due to the analogue properties of the eye [retina]. It is about inserting black space between any two succeeding images, at a subconscious level, which is to be done by setting Timeout at a couple of millisec I guess. Commented Dec 20, 2014 at 4:24

1 Answer 1

1

You should be able to use a pause flag:

var paused = false;

document.onkeydown = function(evt) {
    evt = evt || window.event;
    if (evt.ctrlKey && evt.keyCode == 32) {
        paused = !paused;
    }
};

(function loop() {
  rantime = Math.round(Math.random() * (9000 - 2000)) + 1000;
  setTimeout(function() {
        if (!paused) {
            rotateImage();
        }
        loop();
  }, rantime);
})();
Sign up to request clarification or add additional context in comments.

4 Comments

To save complexity, the pause could be initiated by any key press. (9000 - 2000) can be 7000. ;-)
Hi Delonian, Thank you for your great solution. Sorry for not reacting earlier [serious hartinfarct]. I learned that Firefox is not compatible with keyCode, so it has to alternate with 'event.which'. So for a cross-browser solution I prefer any key. Wimsch
Hi RobG, concerning 'any key press' (including space) you are right, so I corrected it: parsimony fist. However,
However, Math.random() * (9000 - 2000)... doesn't follow from plain arithmatics. This function needs the interval to capture any number in between the upper and lower bound. Wimsch

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.