mr_nobody
mr_nobody

Reputation: 194

analog setinterval in java

I need to check the url (once per second)

Timer timer = new Timer();
    TimerTask task = new TimerTask() {
        public void run()
        {
            System.out.print(engine.getLocation());
        }
    };
timer.schedule( task, 1000 );

but I don't need schedule

In JavaScript it would look like

var interval = self.setInterval(function(){
    console.log(mywindow.location.href);
},1000);

Upvotes: 5

Views: 9365

Answers (2)

Mena
Mena

Reputation: 48434

You can use:

timer.scheduleAtFixedRate(yourTimerTask, new Date(), 1000l);

... which will schedule execution starting now, each second.

Note that the start time and rate are not guaranteed to execute precisely time-wise.

See here for API documentation on the method.

Upvotes: 4

PeterMmm
PeterMmm

Reputation: 24630

timer.schedule( task, 0L ,1000L);

will check every second.

http://docs.oracle.com/javase/6/docs/api/java/util/Timer.html#schedule(java.util.TimerTask,%20long,%20long)

Upvotes: 10

Related Questions