13 Replies Latest reply: May 17, 2006 2:08 AM by Paradigm RSS

    Using an inactivity timer

    Paradigm Community Member
      I have searched the forums for timer hints and tips and although I've tried utilising some suggested code - I can't quite get this to work in my particular project.

      I have a Flash presentation set up for our reception area. It consists of a looped movie clip which acts similar to a screen saver and doubles as a "WELCOME" sign (I have some text input script which places the day's visitors to the building within this "screensaver"). The presentation will also have various user-navigable sections explaining the company history, product ranges, personnel etc.

      What I'd like is that when the user has finished exploring the corporate-based sections and there is no mouse activity for a set time (say 5 minutes) from anywhere in the whole presentation/time line, that Flash will jump back to the "screensaver" frame.

      Can anyone recommend a simple way of achieving this, and does this involve including a "mouse-watch" script on every static frame? I'm from a design background, not a programming one, so if you can help, please explain the script in simple terms!
        • 1. Re: Using an inactivity timer
          blemmo Community Member
          Hi,

          this sure needs some kind of mouse watch script, but you can use onMouseMove and just place the code on the first frame of the main timeline, then it will affect the whole movie.
          Then you'll need an interval the length of the timeout, that calls the 'goto screensaver' action. On mouseMove, clear the existing interval and start a new one. You could use the (undocumented) setTimeout function for the interval (see the comments here).
          So altogether it might be something like this:
          --
          var timeoutID:Number;

          function timedOut () {
          gotoAndPlay("welcome");
          }

          onMouseMove = function(){
          clearTimeout(timeoutID);
          timeoutID = setTimeout(timedOut, 300000); // 300000 ms = 5 min
          }
          --
          That should be all. The code should be placed on the _root timeline's first frame.
          Maybe there's a better way to do this (the timeout is cleared and re-created on every mouse move), but as the mouse move is the event that shall stop the timeout, I don't see a better way here.

          hth,
          blemmo
          • 2. Re: Using an inactivity timer
            Paradigm Community Member
            Cheers Blemmo. I gave this a try and could not get it to work, so needs fine tuning. At the moment the Flash file is using Scenes (I know you all frown on them, but I needed to separate the text input portion from the screensaver portion) and although I tweaked your code to include the Scene name, it doesn't seem to redirect after the timeout (I reduced the timeout to 10 secs to save time during testing). So to recap I have the following Scenes:

            "Input Text" - a scene that consists of some input text boxes, this is so we can add visitor names to the "Screensaver" scene. You add your text, click on a "start" button and the movie jumps to

            "Welcome" - the looped movie that acts like a screensaver, features text (as above) and loops until the user clicks (or touches) the "touch here to begin" button that then jumps to

            "Main" - the scene that will contain all the interactive stuff for the user to browse

            I popped your script onto frame 1 of "Main" thinking that after the mouse inactivity it would jump back to the "Welcome" Scene - the code here is thus:

            var timeoutID:Number;

            function timedOut () {
            gotoAndPlay("Welcome",1);
            }

            onMouseMove = function(){
            clearTimeout(timeoutID);
            timeoutID = setTimeout(timedOut, 10000); // 10000 ms = 10 secs
            }

            What do you think I'm doing wrong? As I've said before, not a programmer so please be patient!
            • 3. Re: Using an inactivity timer
              blemmo Community Member
              To see if the timeOut function executes, add this in the function before the gotoAndPlay line:
              trace("timeout");
              This should echo "timeout" in the output window, when the function executes.

              Currently, you have to move the mouse to start the timeout, maybe you didn't do that? Should be better to start the timeout right after entering the frame. To do that, replace
              var timeoutID:Number;
              with this:
              var timeoutID = setTimeout(timedOut, 10000);

              If the message "timeout" appears, but the movie doesn't jump to the welcome scene, then there's something wrong with the gotoAndPlay function, so you should check if the name of the scene is right. It's case sensitive, so take care of that.

              greets,
              blemmo
              • 4. Using an inactivity timer
                Paradigm Community Member
                Hey Blemmo, I tried your suggestions to tweak the code. The first frame of "Main" now looks like this:

                var timeoutID = setTimeout(timedOut, 10000);

                function timedOut () {
                trace("timeout");
                gotoAndPlay("Welcome",1);
                }

                onMouseMove = function(){
                clearTimeout(timeoutID);
                }

                I tested the movie with the "Output" window open as you suggested, but nothing was displayed in here during the execution of the movie, despite the "trace" line. The "Main" Scene has 500 static frames in it at the moment (with no "stop" command) - more than enough for the time out to take effect before reaching the end and then returning, as logic dictates, to frame 1 of the first Scene ("Input Text"). The "Welcome" Scene name is correct (re case-sensitivity). I also noticed that the mouse activity seems to be ignored, constant moving of the mouse whilst in the "Main" Scene still brings you back to frame 1 of the first Scene once the frames run out.

                Does the code look OK to you?
                • 5. Re: Using an inactivity timer
                  blemmo Community Member
                  Do you use Flash 8? I didn't mention that setTimeout is Flash 8 only, sorry. But it's almost the same as using setInterval, so it's quite easy to change it. Try if this works:
                  --
                  var timeoutID = setInterval(timedOut, 10000);

                  function timedOut () {
                  clearInterval(timeoutID);
                  trace("timeout");
                  gotoAndPlay("Welcome",1);
                  }

                  onMouseMove = function(){
                  clearInterval(timeoutID);
                  timeoutID = setInterval(timedOut, 10000);
                  }
                  --
                  You forgot to start the interval/timeout again after it was stopped onMouseMove in the last code posted, but as said before, if you don't use Flash 8 it won't work at all. The difference to setInterval is only that setTimeout executes the function once, while setInterval executes it repeatedly, so we have to stop the interval after the first execution. And setInterval works since Flash 6.

                  cheers,
                  blemmo
                  • 6. Re: Using an inactivity timer
                    Paradigm Community Member
                    Hey it works! Thank you so much blemmo - you're a star!

                    Yeah, I also forgot to let you know the version I'm running (MX 2004 (v7)), sorry about that, setInterval works fine instead of setTimeout in v7. Just need to set the interval back to 3000000 for 5mins and I'm there.

                    Best regards

                    Paul.
                    • 7. Re: Using an inactivity timer
                      Retroboy_mx
                      Hi Flasher!!!

                      Try with this... It´s a freaky way ...

                      Hope this help you.
                      • 8. Re: Using an inactivity timer
                        Paradigm Community Member
                        Hey Blemmo, I THOUGHT this was working but I have a couple of problems. I have placed the script at Frame 1 in the "Main" Scene as previously suggested:

                        Scenes =
                        "Input Text" - text input field, uses a button to jump to:
                        "Welcome" - animated, looped "screensaver" (1 frame in length), uses a screen sized button (touch anywhere...) to jump to:
                        "Main" - where buttons and various bits of text and images are shown

                        var timeoutID = setInterval(timedOut, 10000);

                        function timedOut () {
                        clearInterval(timeoutID);
                        gotoAndStop("Welcome",1);
                        }

                        onMouseMove = function(){
                        clearInterval(timeoutID);
                        timeoutID = setInterval(timedOut, 10000);
                        }

                        Firstly, when running the movie for the first time, I click/touch the screen on the "Welcome" Scene which takes me to the "Main" Scene - after 10 seconds it does indeed go back to the "Welcome" Scene (the animated wallpaper). However, if I move the mouse continuously whilst on the "Main" Scene, the movie STILL goes back to the "Welcome" Scene! It seems to be disregarding the mouse-movement and just counting down 10 seconds (?).

                        Secondly, when entering the "Main" Scene, a series of buttons are displayed, I use a "stop" action here, waiting for the user to touch one of the buttons. The script attached to Button 1 sends the movie forward to another Frame (120 for example) - I've noticed this jump is interupted and the movie begins at the "Welcome" Scene again, then goes into a wierd loop, playing a bit of "Welcome" and then a bit of "Main".

                        I'm really confused now, any chance you can help out?

                        Flash MX 2004
                        Windows 2000
                        • 9. Re: Using an inactivity timer
                          blemmo Community Member
                          Hi,

                          I'm not very familiar with scenes, probably there are issues with the timeoutID var between different scenes. You can try to trace a message in the mousemove event, to see if it executes. You could also trace the value of timeoutID there, to see if it's defined, and if it's the same timeoutID var created in the first frame.
                          If you want the movie to play without interuption, disable the interval when a button was pressed. Then recreate the interval in the frame where the player stops. Otherwise the timeout will destroy the playback.

                          hth,
                          blemmo
                          • 10. Re: Using an inactivity timer
                            kglad MVP
                            the frame that contains that code can only play once or you'll define intervals that never clear. to remedy you can use the code below. also, you should not use the goto function and you should not use scene info to navigate. use the goto method and frame labels as shown below:

                            p.s. i never heard of setTimeout(). what makes you think that's a flash 8 function blemmo?

                            • 11. Re: Using an inactivity timer
                              blemmo Community Member
                              hey kglad,

                              according to the comments in the LiveDocs ( http://livedocs.macromedia.com/flash/8/main/00001717.html), setTimeout() was introduced in Flash 8, but accidentally not documented. I'm not sure if it's really necessary when there is setInterval(); it just makes clearInterval() in the called function redundant, as I see it, but it may avoid errors for lazy programmers .
                              It's also featured in AS 3 ( http://livedocs.macromedia.com/labs/1/flex/langref/flash/util/package.html#setTimeout()), so I guess it was really just an accident that it is undocumented.

                              greets,
                              blemmo
                              • 12. Re: Using an inactivity timer
                                kglad MVP
                                hey thanks blemmo. and i thought you were crazy when i saw you using setTimeout().
                                • 13. Re: Using an inactivity timer
                                  Paradigm Community Member
                                  Thanks kglad - your code works absolutely brilliantly, placed just once on the timeline at Frame 1.