JavaScript - SetInterval doesn't function properly

前端 未结 4 1753
忘掉有多难
忘掉有多难 2021-01-13 22:40

I got this piece of script (runned locally):



        
4条回答
  •  终归单人心
    2021-01-13 22:53

    You need to pass a reference to the function instead of calling it.

    This:

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

    should be:

    setInterval (uploadnew, 1000*60*5);
    

    If you were to call it as you were, you would need to have uploadnew() return a function to be passed to setInterval.

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

    Note the change to the recursive call.

提交回复
热议问题