简体   繁体   English

setInterval(function, timer) 点击回车键变成无限循环

[英]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.我有我的设置间隔 function 并且它工作正常,但有时在触发 setintervel 并且我按 Enter 键大约 4 到 5 次后,我的间隔变得无限。 can any one help me on this.谁可以帮我这个事。

Thanks in advance.提前致谢。

code: / one of the js file /代码:/其中一个js文件/

 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 page.jsp:初始化方法我添加了隐藏变量并设置值

First of all, the setInterval Method needs a function as first parameter, not "function()".首先,setInterval 方法需要一个 function 作为第一个参数,而不是“function()”。 For example:例如:

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

You can also use the following syntaxe to declare your function ouside the timer:您还可以使用以下语法在计时器之外声明您的 function:

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我希望它有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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