JavaScript - SetInterval not working properly

I got this piece of script (running locally):

<script>

last = 0;

function uploadnew(){

var randomnumber=Math.floor(Math.random()*6);
if(randomnumber != last){
    document.forms['f'+randomnumber].submit();
} else { uploadnew(); }

}

setInterval (uploadnew(), 1000*60*5);

</script>

But it seems that setInterval is not working / the form submit function is not working ...

Any help would be appreciated!

Thank!

+3
source share
4 answers

You need to call setInterval()without parentheses in your function, for example:

setInterval(uploadnew, 1000*60*5);

Using parentheses, you call it immediately and assign the result ( undefined) to start at intervals, instead use parentheses to pass the function itself, not the result of the function.

+9
source

You need to delete ()after uploadnewin the call to setInterval:

setInterval (uploadnew, 1000*60*5);

JavaScript , . setInterval, , .

setInterval ("uploadnew()", 1000*60*5); , "" eval. Eval - , , .

+5

.

:

setInterval (uploadnew(), 1000*60*5);

:

setInterval (uploadnew, 1000*60*5);

, , uploadnew() , setInterval.

function uploadnew() {
    return function(){
        var randomnumber=Math.floor(Math.random()*6);
        if(randomnumber != last) {
            document.forms['f'+randomnumber].submit();
        } else { uploadnew()(); }
    }
}

.

+3

setTimeout ( expression, timeout );
+2

Source: https://habr.com/ru/post/1772371/


All Articles