Is SetTimeout / clearTimeout dangerous?

I wanted to ask if there is another implementation of setTimeout / clearTimeout to replace such a nested structure, avoiding feedback

function timedCount()
{
    document.getElementById('txt').value=c;
    c=c+1;
    t=setTimeout("timedCount()",1000);
}

function stopCount()
{
    clearTimeout(t);
    timer_is_on=0;
}

I read too dangerous to have an infinite nested loop, because at an unspecified moment the client will crash due to insufficient memory.

I also want to ask. What happens to the method clearTimeout()? Does it clear the memory stack?

+5
source share
4 answers

"" - ( ) , , :

function timedCount()
{
    document.getElementById('txt').value=c;
    c=c+1;
    window.t=setTimeout( timedCount, 1000 );
}

function stopCount()
{
    clearTimeout(window.t);
    timer_is_on=0;
}

, setInterval, setInterval , ...

(function updatePage(){
throw new Error( "computer is not turned on" );
setTimeout( updatePage, 1000 );
})()

function updatePageDumb(){
throw new Error( "computer is not turned on" );
}

setInterval( updatePageDumb, 1000 );
+4

setInterval clearInterval ?

+1

setTimeout , , , . setTimeout , . .

0

:

1) dom, , DOM.

jQuery, $(document).ready().

2) clearTimeout t , . t , timeout - .

3) timedCount() , JS eval, , . http://www.jslint.org. JS eval. , timedCount().

4) For another implementation, it really depends on how and when you want your function to be stopCount()called. In your implementation, it will never be called, as it continues to call the same function after an interval of 1 second.

The desired code may be something like

function timedCount()
{
    // the first 2 lines doing something
    t = setTimeout(function()
    {
        // if clear time out logic
        if ( can_clear_timeout() )
        {
            stopCount();
        }
        else
        {
            return timedCount();
        }
    }, 1000);
}

5) Another warning: you can not use global variables, such as timer_is_on, as this is contrary to standards.

0
source

All Articles