我正在处理一个div(父),它有两个其他div(菜单和内容),如下所示:
<div id="parent">
<div id="menu"></div>
<div id="content"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
内容div中加载的内容是一个html文件,它具有一些javascript函数,例如每5秒重新加载其内容的自动刷新.
$(document).ready(function () {
setInterval(function () {
grid.reloadDefaultContent(); //this reloads the content on content div.
}, 5000);
}
Run Code Online (Sandbox Code Playgroud)
页面上有一些链接将不同的内容加载到内容div中.到目前为止一直很好,直到我回到"家",它具有自动刷新功能.问题是自动刷新从未停止过,现在我点击再次回到家中,该功能正在运行两次(或者我改变页面并返回主页的次数),将会话放在上面.我正在使用jQuery加载页面$.load().
关于如何使setInterval实例的任何想法只运行一次?
编辑:在看完我自己的问题之后,我看到它有点不清楚.我的主要问题是,当我回到家时,我的第一个setInterval实例已经运行,并且第二个实例启动,使得5秒的自动刷新变得更快,每次我都会越来越快更改页面并返回主页.这不是我想要的行为.我需要的是当我更改div上的内容时停止setInterval的实例,并在我回到主页时重新启动它.
该函数showRandom每1000毫秒执行一次,但我希望它每random毫秒执行一次..有什么解决办法吗?谢谢!
var random = 1000;
setInterval(function() {random = randomizator(60000,200000);} ,1000);
setInterval(function() {showRandom(random);}, random);
function randomizator(a,b)
{
return Math.floor(Math.random()*b) + a;
}
function showRandom(random)
{
$('#test').text(random);
}
Run Code Online (Sandbox Code Playgroud)
演示:jsFiddle
我正在使用 javascript setInterval() 检查用户空闲时间并在自动注销之前显示弹出警报。但它不适用于多个选项卡(适用于单个选项卡)
以下是我的代码:
localStorage.removeItem("idleTimeValue");
var idleInterval = setInterval(timerIncrement, 1000);
function timerIncrement()
{
if(localStorage.getItem("idleTimeValue")) {
idleTime = parseInt(localStorage.getItem("idleTimeValue")) + 1; //increments idle time by one second
} else {
idleTime = 1;
}
localStorage.setItem("idleTimeValue", idleTime);
var timeDiff = 600;
var totTimeRemaining = timeDiff-idleTime;
if(totTimeRemaining > 0) {
$('#timeoutWindow').modal('show');
var minutes = Math.floor(totTimeRemaining / 60);
var seconds = totTimeRemaining - minutes * 60;
$('#timeoutRemainingTime').html(minutes+" minutes and "+seconds+" seconds");
} else {
window.location = httpHost+"/account/index/logout";
}
}
$(this).click(function (e)
{
localStorage.removeItem("idleTimeValue"); …Run Code Online (Sandbox Code Playgroud) 我使用以下方法开发了一个javascript聊天(后端的php):
1) long-polling to get new messages for the receiver
2) sessionStorage to store the counter of messages
3) setInterval to read new messages and if sessionStorageCounter < setIntervalCounter then the last message is shown to receiver.
4) javascript to create,update and write the chat dialogues
Run Code Online (Sandbox Code Playgroud)
该模块工作正常,但当用户快速聊天时,接收者的前端会收到两到三条相同的消息,(计数器都不会失败,查询也不会提供双重插入).
代码似乎是正确的(这就是我不提供代码的原因),因此间隔延迟可能是原因(在减少间隔延迟时,没有任何变化).
您是否认为上述架构是一种不好的做法,您认为哪种架构可以消除错误?
我正在尝试使用该函数在 javascirpt 和 jQuery 中制作一个计时器setInterval。计时器应从 90 倒计时到零(秒)。
我为此使用的代码:
setInterval(settime(), 1000);
Run Code Online (Sandbox Code Playgroud)
在此settime()设置 var time(从 90 开始)-1,此操作必须每秒发生一次。我的问题是我不知道如何让这个动作发生 90 次;我尝试使用 for 循环,但计数器在 1 秒内从 90 计数到 0。
有谁知道更好的选择吗?
编辑:我所说的多次触发的意思是 newjob() 将每 5 秒触发 3 次...所以在 20 秒内我将触发它 12 次,而不是我想要的 4 次。所以它每 5 秒触发多次,而不是每 5 秒触发一次。
我有一个使用 Toastr 创建的函数,用于在我的 Web 应用程序上显示消息。我最终会将其与 API 的 ajax 请求联系起来,以确定是否显示消息,但现在我只是测试它的外观。
我正在设置一个间隔,但它会多次触发其中的函数(通常为 3 次)。
$(document).ready(function() {
setInterval(function () {
newJob();
}, 5000);
});
Run Code Online (Sandbox Code Playgroud)
我无法执行 setInterval( function(e) { } 因为 e 未定义,因为没有与之关联的事件,点击时,我使用了 e.stopImmediatePropagation(); 让它只触发一次。我怎样才能如果我没有 e,则按设定的时间间隔停止立即传播?
谢谢。
编辑:完整代码:
var newJob = function(e) {
var i = -1;
var $toastlast;
var getMessage = function () {
var msgs = ["There's a new job in the job dispatch queue", …Run Code Online (Sandbox Code Playgroud) 我有一个 setInterval 函数,每小秒运行一次。现在,我正在浏览器中探索我的控制台,我发现 setInterval 函数中的函数有时会运行两次。我怎样才能防止运行两次?
这是我的设置间隔:
$('#myclinic_id').change(function(){
clearInterval(interval);
lastQueueID = 0;
$("#boxqueue").empty();
var selectedClinicID = $(this).val();
clinicID = selectedClinicID;
statusClinic(clinicID, userID);
show_patients(clinicID, userID);
if(selectedClinicID != "0" || selectedClinicID != undefined){
interval = setInterval(function(){
check_getqueue(clinicID, userID);
}, 4000);
}
});$('#myclinic_id').change(function(){
clearInterval(interval);
lastQueueID = 0;
$("#boxqueue").empty();
var selectedClinicID = $(this).val();
clinicID = selectedClinicID;
statusClinic(clinicID, userID);
show_patients(clinicID, userID);
if(selectedClinicID != "0" || selectedClinicID != undefined){
interval = setInterval(function(){
check_getqueue(clinicID, userID);
}, 4000);
}
});
Run Code Online (Sandbox Code Playgroud)
现在,在check_getqueue函数内部我还有一个函数,我想防止它运行两次,这是我出现的问题。这是我在 check_getqueue 函数中的代码,其中在 check_getqueue 函数中命名的函数refresh_afterdel(clinicID, userID); …
我必须在我的应用程序中检测用户的空闲超时。我的应用程序同时使用本机 JavaScript 和 Angular。在我的 HTML + JavaScript 页面中,我实现如下:
<script>
var inactivityTime = function () {
var time;
document.onmousemove = resetTimer;
document.onkeypress = resetTimer;
document.onclick = resetTimer;
document.onwheel = resetTimer;
function callApi() {
alert("Idle for 3 seconds. You can call your api here");
}
function resetTimer() {
clearTimeout(time);
time = setTimeout(callApi, 3000);
// 5 minute = 60000 * 5;
// 1000 milliseconds = 1 second
}
};
window.onload = function() {
inactivityTime();
}
</script>
Run Code Online (Sandbox Code Playgroud)
它正在按预期工作。
虽然我在角度项目中使用的相同,角度代码也可以工作,但观察到 1 个小问题,请先查看我的角度代码:
import { Component, …Run Code Online (Sandbox Code Playgroud) 我遇到的问题是,离开组件时我使用的间隔useEffect没有停止。
代码是:
useEffect(() => {
const timer = window.setInterval(() => {
refreshSave();
}, 3000);
return () => {
window.clearInterval(timer);
};
}, []);
Run Code Online (Sandbox Code Playgroud)
我正在考虑制作一个空的动画span,然后用于onAnimationIteration触发保存。
我希望字母“q”每秒只写一次。但它每秒都会翻倍。如何用这个命令来做到这一点?
function writeNow() {
document.write('q');
setInterval(writeNow, 1000);
}
writeNow();Run Code Online (Sandbox Code Playgroud)
javascript ×10
setinterval ×10
jquery ×4
ajax ×1
angular ×1
cleartimeout ×1
long-polling ×1
onready ×1
php ×1
random ×1
react-hooks ×1
reactjs ×1
tabs ×1