Results 1 to 3 of 3
  1. #1
    D.Calladine is offline Member
    Join Date
    Nov 2010
    Posts
    11
    Rep Power
    0

    Default run a method with timer that only runs when nothing is happening

    Hi guys im looking or anyone that can point in the right direction when it come to timers

    i have a method that closes one window and opens another.

    but im looking to make it so this methods only runs after a certain amount of time has passed

    and this times only starts when nothing is happening

    for example, i want this method to run after 1 minute.

    so i press a button, then this timer starts because nothing is happening but if another button is pressed the timer is restarted but if nothing is pressed for that whole minute it runs the afore mentioned method.

    Thank you for any info

  2. #2
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

    Default Re: run a method with timer that only runs when nothing is happening

    Have you looked into using System.currentTimeMillis();

    You could record the start time and once every pass of a loop have another variable(timePassed) that is equal to the current time minus the start time.
    Then check that timePassed was not more than 1 minute's worth of milliseconds.

  3. #3
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: run a method with timer that only runs when nothing is happening

    Keep a class field for the last time the user was active
    private long lastActive;

    Create a setter to update the field
    private void setLastActive() { lastActive = Calendar.getInstance.getTimeInMillis(); }

    Add the setter at the end of each action
    public void ActionMethod() {
    ...
    setLastActive();
    }

    Add a class field to define the timeout after which the user is deemed inactive
    private final long inactivePeriod = 1*60*1000; //1min

    Create a method to check for inactivity
    private boolean isActive() {
    return (lastActive-Calendar.getInstance().getTimeInMillis() >= inactivePeriod);
    }

    Create a method to deal with inactivity
    private void ifInactiveStart() {
    if (!isActive()) {
    //create a timer
    ...
    }
    }

    Listen for inactivity.... this could be done in any method called regularly but probably best done whenever the value is changed, so we alter the setter
    private void setLastActive() {
    ...
    ifInactiveStart();
    }

    Create a timer task
    Timer t = new Timer(); //java.util.Timer
    t.schedule(new TimerTask() {
    public void run() { ... }
    }, inactiveTimerDelay);

Similar Threads

  1. Replies: 1
    Last Post: 06-07-2011, 07:22 AM
  2. I don't know what is happening
    By Fred1 in forum New To Java
    Replies: 2
    Last Post: 04-27-2011, 11:21 PM
  3. Stopping a Timer from Inside the timer
    By krishnan in forum Java Applets
    Replies: 2
    Last Post: 10-04-2010, 11:15 PM
  4. countdown timer, little help with method
    By sidy in forum New To Java
    Replies: 22
    Last Post: 07-19-2008, 12:42 PM
  5. Replies: 0
    Last Post: 04-04-2008, 02:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •