我有一个循环功能,它在前5秒内运行social1(),在第二个5秒内运行social2()然后循环...
我也有2个悬停功能
我需要清除所有活动超时,因为当我将鼠标悬停在图像(.social1和.social2)上时,我可以看到多个超时正在运行
怎么解决这个问题?
function social1() {
$('.social1').fadeTo(500, 1);
$('.social2').fadeTo(500, 0.5);
timeout = setTimeout(function() {
social2();
}, 5000);
}
function social2() {
$('.social1').fadeTo(500, 0.5);
$('.social2').fadeTo(500, 1);
timeout = setTimeout(function() {
social1();
}, 5000);
}
$(document).ready(function ()
{
social1();
$('.social1').hover(
function () {
window.clearTimeout(timeout);
social1();
},
function () {
timeout = setTimeout(function() {
social2();
}, 5000);
}
);
$('.social2').hover(
function () {
window.clearTimeout(timeout);
social2();
},
function () {
timeout = setTimeout(function() {
social1(); …Run Code Online (Sandbox Code Playgroud) 这是组件:
class ChartComp extends Component{
constructor(props){
super(props);
this.timer = null;
this.loadData = this.loadData.bind(this);
}
componentWillMount(){
this.loadData();
}
componentWillUnmount(){
if(this.timer){
clearTimeout(this.timer);
}
}
loadData(){
//...
getJSON(url, msg=>{ //get data from server
if(msg.success){
//...
this.timer = setTimeout(()=>{this.loadData()}, 30000); //get data and rerender the component every 30s
}
})
}
render(){
//...
}
}
Run Code Online (Sandbox Code Playgroud)
在卸载组件之前将调用clearTimeout函数.但是计时器处于异步功能,它在我收到服务器响应后再次启动.那么我怎样才能使clearTimeout工作?
我尝试在启动新超时之前清除以前的超时,因为我希望消息显示4秒钟,然后消失,除非在4秒钟之前弹出新消息。问题:在这种情况下,旧超时正在清除当前消息,因此clearTimeout()在此组件中不起作用:
let t; // "t" for "timer"
const [message, updateMessage] = useState('This message is to appear for 4 seconds. Unless a new message replaces it.');
function clearLogger() {
clearTimeout(t);
t = setTimeout(() => {
console.log('wiping message');
updateMessage('');
}, 4000);
}
function initMessage(msg) {
updateMessage(msg);
clearLogger();
}
Run Code Online (Sandbox Code Playgroud)
有趣的是,这有效:
function clearLogger() {
t = setTimeout(() => {
console.log('wiping message');
updateMessage('');
}, 4000);
clearTimeout(t);
}
Run Code Online (Sandbox Code Playgroud)
...但显然无法达到目的,因为它立即消除了超时。在实践中,我应该能够每两秒钟触发一次initMessage(),并且永远不会看到“擦除消息”记录到控制台。
clearTimeout()内部for循环不起作用
for(i=0;i<10;i++){
myVar = setTimeout(function(){
alert("Hello")
}, 3000);
}
Run Code Online (Sandbox Code Playgroud)
小提琴:不工作
小提琴:工作
请帮我停setTimeout()在第一个小提琴.
我必须在我的应用程序中检测用户的空闲超时。我的应用程序同时使用本机 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) 但每次用户点击页面上的任何内容时,剩余时间必须再次重置为 30 秒(或者作为另一个选项,将使用clearTimeOut 和 setTimeOut。)同时,当用户没有点击页面上的任何内容时,他们通过删除 accessToken,将在 30 秒后自动注销。
所以为了解决这个问题,我的方法是这样的:
当用户访问此页面时,setTimeout() 将开始。
当用户单击页面上的某些内容时,clearTimeOut 将被激活,setTimeOut 也将再次被激活
当用户30秒内没有点击页面上的任何内容时,他们将通过删除本地存储中的accessToken自动注销
目前,通过删除 accessToken 可以让用户在 30 秒后注销!UseEffect 中的 setTimeOut 也有效。
唯一的问题是我不知道当用户在页面上单击事件时如何使clearTimeOut()和setTimeOut工作。
import styled from 'styled-components';
import React, {useRef, useEffect, useState} from 'react';
import ScreenContainer from '../../src/component/common/ScreenContainer';
import {useNavigate as useDomNavigate} from 'react-router-dom';
import isNil from 'lodash/isNil';
import userClient from '../../src/client/user/userClient';
const Device = () => {
const domNavigate = useDomNavigate();
const [storeId, setStoreId] = useState(() => JSON.parse(localStorage.getItem('storeId')));
const [currentDeposit, setCurrentDeposit] = …Run Code Online (Sandbox Code Playgroud) 我正在尝试在文本输入字段上设置超时,该字段仅在用户停止输入后的第二秒内实现内部代码.因此,当用户键入时,我会不断调用cleartimeout并重新启动setTimeout.
我最初在Objective C中查看performSelector函数,但看起来没有Swift等价物.
然后我转到Swift中的GCD函数,寻找执行此操作的方法.
这是我想出的:
var delta: Int64 = 1 * Int64(NSEC_PER_SEC)
var time = dispatch_time(DISPATCH_TIME_NOW, delta)
dispatch_suspend(dispatch_get_main_queue())
dispatch_after(time, dispatch_get_main_queue(), {
println("timeout")
});
Run Code Online (Sandbox Code Playgroud)
dispatch_suspend函数不能像我希望的那样工作.
也许这里的派遣功能不合适?
我使用的是sinon用伪造的计时器,我要检查,如果clearTimeout被称为具有特定的超时-ID.
var clock = sinon.useFakeTimers();
functionUnderTest();
// How can I know if functionUnderTest cleared a specific timeout?
Run Code Online (Sandbox Code Playgroud) 我用setTimeout在10秒后跳转到上传页面。但是当用户导航到另一个页面时,超时仍然存在。我按照此链接上的说明操作(Angular 2 setinterval() keep running on other component)并将clearTimeout添加到ngOnDestroy,但它不起作用。
ngOnInit() {
// Get uploaded file info
this.uploadService.getFileInfo().subscribe((value) => {
this.fileInfo = value;
this.http.post(environment.apiUrl+'/api/dataValidation/validateData.php', this.fileInfo)
.subscribe(res => {
console.log('response'+res)
if (res['length']<5) {
//console.log(res);
this.failure = true;
this.fileValidationResults.errorMessage =res[0];
this.loading = false; // stop loading screen
this._timer =setTimeout(() => {
this.router.navigate(["/uploads/upload"]);
}, 10000);
}
})
});
}
ngOnDestroy() {
if (this._timer){
clearTimeout(this._timer);
};
}
Run Code Online (Sandbox Code Playgroud)
当用户导航到另一个链接时如何停止 setTimeout() 并让 setTimeout() 仅在其自己的组件中工作?
我正在创建一个“高级”井字游戏,其中每转10秒后消失(即:放置的每个X或O在10秒后恢复为空白方块)。一切正常,但是如果用户决定取消当前游戏并开始新游戏,则会出现问题。
如果用户开始一个新游戏并单击以前占用的方块,则超时功能将根据先前游戏的点击清除该方块,即在不到10秒的时间内。
使用clearTimeout可以为SetTimeout的最新实例重置计时器,但是如果用户重置游戏板时选择了多个方块,则无济于事。而且由于setTimeout应用于onclick函数中的每个X和O,所以我没有办法跟踪多个实例ID。
任何想法将不胜感激,下面的代码。
编辑:您可以在此处查看游戏的实时版本(WIP):http : //kylechadha.com/projects/tic-tac-toe/
全局变量:
var elements = document.getElementsByClassName('cell');
var rows = document.getElementsByClassName('row');
var alternate = 0;
var counter = 0;
var timerX; // Handles setTimeout instances for 'X'
var timerO; // Handles setTimeout instances for 'O'
Run Code Online (Sandbox Code Playgroud)
设置X和O的功能:
var play = function() {
for (i = 0; i < elements.length; i++) {
elements[i].onclick = function () {
if (this.className[0] == "c" && win == false) {
if (alternate % 2 == 0) {
this.className …Run Code Online (Sandbox Code Playgroud) cleartimeout ×10
javascript ×7
settimeout ×7
reactjs ×3
angular ×2
jquery ×2
asynchronous ×1
function ×1
logout ×1
setinterval ×1
sinon ×1
swift ×1
timeout ×1
typescript ×1
unit-testing ×1