我正在尝试使用该函数在 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); …
需要P[n]Y[n]M[n]DT[n]H[n]M[n]S在 PostgreSQL 中以 ISO 8601 格式存储持续时间,然后以相同格式在脚本中检索它。在 PostgreSQL 中存储 ISO 8601 持续时间的数据类型是什么?
我必须在我的应用程序中检测用户的空闲超时。我的应用程序同时使用本机 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触发保存。
let updateTimer: number;
export function Timer() {
const [count, setCount] = React.useState<number>(0);
const [messages, setMessages] = React.useState<string[]>([]);
const start = () => {
updateTimer = setInterval(() => {
const m = [...messages];
m.push("called");
setMessages(m);
setCount(count + 1);
}, 1000);
};
const stop = () => {
clearInterval(updateTimer);
};
return (
<>
<div>{count}</div>
<button onClick={start}>Start</button>
<button onClick={stop}>Stop</button>
{messages.map((message, i) => (
<p key={i}>{message}</p>
))}
</>
);
}
Run Code Online (Sandbox Code Playgroud)
代码示例:https : //codesandbox.io/s/romantic-wing-9yxw8?file=/ src/ App.tsx
该代码有两个按钮 - 开始和停止。
开始调用 asetInterval并保存间隔 ID。定时器设置为 …
我有一个简单的组件,useState每次点击都会增加一个计数器 -
function Counter() {
let [counter, setCounter] = useState(0);
const incCounter = () => {
setCounter(counter + 1);
};
return (
<div className="App">
<h1>{counter}</h1>
<button onClick={incCounter}>Inc</button>
</div>
);
}
Run Code Online (Sandbox Code Playgroud)
现在我想每 1 秒调用一次增加函数,所以我将这段代码添加到组件函数中 -
useEffect(() => {
setInterval(() => {
incCounter();
}, 1000);
}, []);
Run Code Online (Sandbox Code Playgroud)
但我没有看到组件中的计数器增加。
我应该如何正确编写它并看到计数器按预期每 1 秒增加一次?
我希望字母“q”每秒只写一次。但它每秒都会翻倍。如何用这个命令来做到这一点?
function writeNow() {
document.write('q');
setInterval(writeNow, 1000);
}
writeNow();Run Code Online (Sandbox Code Playgroud)
我在Win 7的每个浏览器中都测试了这个脚本.它不仅适用于Firefox(版本3.6.13).
除了返回随机数的Firefox之外,警报框在所有浏览器中都返回"undefined".这是脚本
function nextSlide(nav){
alert(nav);
}
jQuery(function(){
var set = setInterval(nextSlide, 2000);
});
Run Code Online (Sandbox Code Playgroud)
这是一个现场演示
setinterval ×10
javascript ×9
jquery ×3
reactjs ×3
react-hooks ×2
angular ×1
cleartimeout ×1
duration ×1
ecmascript-6 ×1
firefox ×1
intervals ×1
iso8601 ×1
onready ×1
postgresql ×1
use-state ×1