Resetting a Settimeout

Resetting a setTimeout

You can store a reference to that timeout, and then call clearTimeout on that reference.

// in the example above, assign the result
var timeoutHandle = window.setTimeout(...);

// in your click function, call clearTimeout
window.clearTimeout(timeoutHandle);

// then call setTimeout again to reset the timer
timeoutHandle = window.setTimeout(...);

How do I properly restart a timeout in Javascript?

Here's the problem:

t = setTimeout(carousel, 5000); 
var interval;
$(document).on('mousemove keyup keypress',function(){
clearTimeout(t /* instead of carousel */);
t = setTimeout(carousel, 6000); // also refresh the value of the timeout
})

Reset setTimeout() back to maximum time without executing?

Set the timer as a global variable, clear the previous timer on click

var timer;
button.onClick(
clearTimeout(timer);
timer = setTimeout(func(){}, 1000);
);

How to reset a setTimeout if you receive data while time is running?

i think what you are trying to achieve here is debouncing if i'm correct you can do it like this for every kry press we create a timeout and if the key not pressed for certain time the method in settimeout will be called

var timeoutId = 0;
function keypress() {
if (timeoutId >= 0) {
clearTimeout(timeoutId);
}
timeoutId = window.setTimeout(() => {
callMethod();
}, 500);// time to wait to listen for next keypress if not pressed callMethod will execute
}

How to reset timeout in Javascript/jQuery?

A timer can be cancelled with clearTimeout, so:

var timer = null;
if (timer) {
clearTimeout(timer); //cancel the previous timer.
timer = null;
}
timer = setTimeout(thisFunction, 10000);

setTimeout and clearTimeout in React

You could do something like this. So, you run the timer and every time you clicking somewhere in the document it will cancel the current timer and run the new. So your myFunc function will run only if user doesn't click on the page after N seconds.

Keep in mind, you want to do it in 30 sec you will need to put 30000 in setTimeout

const timerId = useRef(null)

const myFunc = () => {
clearTimeout(timerId.current)
timerId.current = null
console.log('DO SOMETHING')
}

const onDocumentClick = () => {
if (timerId.current) {
clearTimeout(timerId.current)
timerId.current = window.setTimeout(myFunc, 3000)
}
}

useEffect(() => {
timerId.current = window.setTimeout(myFunc, 3000)

document.addEventListener('click', onDocumentClick)
return () => {
clearTimeout(timerId.current)
document.removeEventListener('click', onDocumentClick)
}
}, [])

Interrupt and Reset set Timeout - Javascript

  1. timer needs to be outside the function
  2. test needs to be its own function

let timer;
const test = () => {

if (count < 6) {
const currentquestion = questions[count];
//IGNORE THIS PART
if (count <= 1) {
let randomizer = Math.random();
randomizer <= 0.5 ?
(question = currentquestion.easy) :
(question = currentquestion.hard);
} else {
scores[scores.length - 1] >= 0.65 ?
(question = currentquestion.hard) :
(question = currentquestion.easy);
}
//CONTINUE HERE
console.log(`${question}`);
timer = setTimeout(test, 2000); // set it to something shorter while testing

} else {
clearTimeout(timer);
console.log(Done);
}
count++;
};

const enter = document.addEventListener("keydown", function(e) {
if (e.key === "Enter") {
clearTimeout(timer);
test()
}
});
test(); // start
<script>
// moved the setup
const Question1E = " 1-This is the easy Question";
const Question1H = " 1-This is the hard Question";
const Question2E = " 2-This is the easy Question";
const Question2H = " 2-This is the hard Question";
const Question3E = " 3-This is the easy Question";
const Question3H = " 3-This is the hard Question";
const Question4E = " 4-This is the easy Question";
const Question4H = " 4-This is the hard Question";
const Question5E = " 5-This is the easy Question";
const Question5H = " 5-This is the hard Question";
const Question6E = " 6-This is the easy Question";
const Question6H = " 6-This is the hard Question";
const Done = " DONE ";

let count = 1;
let question = null;
const scores = [];

const questions = {
1: {
intent: "Intent1",
easy: Question1E,
hard: Question1H,
},
2: {
intent: "Intent2",
easy: Question2E,
hard: Question2H,
},
3: {
intent: "Intent3",
easy: Question3E,
hard: Question3H,
},
4: {
intent: "Intent4",
easy: Question4E,
hard: Question4H,
},
5: {
intent: "Intent5",
easy: Question5E,
hard: Question5H,
},
6: {
intent: "Intent6",
easy: Question6E,
hard: Question6H,
},
};
</script>


Related Topics



Leave a reply



Submit