简体   繁体   中英

setInterval(function, timer) become to infinite loop when click Enter key

I have my set interval function and its working fine, but sometimes after setintervel trigged and I pressed Enter key some 4 to 5 times and my interval become infinite. can any one help me on this.

Thanks in advance.

code: / one of the js file /

 var intervaltime=setInterval(functionname(), 1000);
    functionname()
    {
    if(pageloaded== "true"){  //pageloaded is coming from one JSP when I click that page.jsp
            clearInterval(intervaltime);
            }
    }
**//page.jps**
<input type="hidden" value="true" id="pageloaded" name="pageloaded" />

page.jsp: init method I added the hidden variable and to set the value of

First of all, the setInterval Method needs a function as first parameter, not "function()". For example:

setInterval(function() {
    console.log("hello");
}, 1000);

You can also use the following syntaxe to declare your function ouside the timer:

function yourfunc() {
    // ...
}
setInterval(yourfunc, 1000);

Also, I think that what you are trying to do is to stop something when your input is clicked or when your page is loaded. If so, the timer is not a good way to do that. You should use an event listener instead.

I hope it helped

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM