خرید بک لینک

Vote count: 0

This is something that works in every browser I've tested: starting a timer with setInterval and stopping it with clearTimeout, or conversely staring with setTimer and stopping with clearInterval.

function countUp()
{
  ++counter.textContent;
}

var timer;

setIntervalButton.onclick = function ()
{
  if (!timer)
    timer = setInterval(countUp, 1000);
};

clearTimeoutButton.onclick = function ()
{
  clearTimeout(timer);
  timer = undefined;
};
<div>Count: <span id='counter'>0</span></div>
<button id='setIntervalButton'>
setInterval
</button>
<button id='clearTimeoutButton'>
clearTimeout
</button>

It's my impression that clearInterval and clearTimeout both point to the same function. Is this behavior guaranteed by the specification, or is it a mere implementation accident that both functions can be used interchangeably?

If the functions are not identical, then is it safe to stop a timer with

clearInterval(timer);
clearTimeout(timer);

if one doesn't know how it was started?

I'm asking because I was optimizing some code, and I thought it would make things a bit easier if one could use the same function to stop a timer regardless of whether it's a repeating one or not.

asked 2 mins ago

برچسب: نویسنده: استخدام کار تاريخ: جمعه 8 مرداد 1395 ساعت: 14:07

صفحه بندی