/    Sign up×
Community /Pin to ProfileBookmark

interval timers and the value of them…

Here is a bit of a conundrum, it you have an interval timer and it is interrupted say by a clearInterval call, how can you tell how far along the interval the timer was when it was canceled?

When I look at an object that is an setInterval() event timer, I get -1 as the output when I valueOf() the variable.

What I am wondering is if for example the following

[CODE]var thisIntervalTimer = setInterval(“onceAminute()”,60000);[/CODE]

the interval of one minute passes between updates, apart from setting up a tracking variable to track milliseconds, does the thisIntervalTimer possess any user accessible values like [B]thisIntervalTimer.getMilliseconds;[/B] to return the current ‘tick’ value of the interval timer attached to the variable thisIntervalTimer.

What I would be interested in is if any method exists to access any properties of this object, they must exist in some form as how would the browser know what event timer to update or trigger.

So somewhere this value must be accessible.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@aj_nscNov 27.2011 — <i>
</i>var timer = false,
remaining = 6000;

start();

function stop() {
var now = new Date();
remaining = now.getTime()-timer.getTime()

//if remaining is less than or equal to 0, then reset it back to 6000
if(remaining &lt;= 0) { remaining = 6000; }

clearInterval(interval);

}

function start() {
timer = new Date();
interval = setInterval(function() { doSomething(); },remaining);
}


The above code demonstrates the idea of what you are asking for, however, I agree with your idea, it'd be great if the interval/timeout had some public interface that allowed you determine how far along it had gotten.
Copy linkTweet thisAlerts:
@Declan1991Nov 27.2011 — BTW, using setTimeout with a string is exactly the same as using eval, it's better (from a scope point of view primarily) to pass a function.

thisIntervalTimer only holds an id value, if cleared it'll be a negative integer, if not cleared, a positive integer as far as I can remember.
×

Success!

Help @JunkMale spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.8,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...