How do I delay a function call for 5 seconds?
You can use plain javascript, this will call your_func once, after 5 seconds:
setTimeout(function() { your_func(); }, 5000);
If your function has no parameters and no explicit receiver you can call directly setTimeout(func, 5000)
There is also a plugin I've used once. It has oneTime
and everyTime
methods.
- jQuery timers plugin
How to delay JS function from running until 5 seconds after page loads
Since you want the function to be fired up 5 seconds after the page is fully loaded you will be using a combination of two functions.
I see you are using jQuery in your website
The below code waits until the page is fully loaded then fires up the code inside the brackets.
$( document ).ready(function() {
// code here
});
So inside the above code you will add your 5 seconds waiting function
setTimeout(function(){
// Magic happens here
},5000);
The final code is
$( document ).ready(function() {
setTimeout(function(){
// Magic happens here
},5000);
});
How to wait 5 seconds with jQuery?
Built in javascript setTimeout.
setTimeout(
function()
{
//do something special
}, 5000);
UPDATE: you want to wait since when the page has finished loading, so put that code inside your $(document).ready(...);
script.
UPDATE 2: jquery 1.4.0 introduced the .delay
method. Check it out. Note that .delay only works with the jQuery effects queues.
Delay first function call for n-seconds
I guess you are not waiting until timeout to declare the interval function.
There is a few different ways to do that, so let me suggest one:
function ping() {
// code ...
}
setTimeout(function () {
ping(); // call function when page loads for first time
// call function every 30 seconds
window.setInterval(function(){
ping();
}, 30000);
}, 5000);
How do I add a delay in a JavaScript loop?
The setTimeout()
function is non-blocking and will return immediately. Therefore your loop will iterate very quickly and it will initiate 3-second timeout triggers one after the other in quick succession. That is why your first alerts pops up after 3 seconds, and all the rest follow in succession without any delay.
You may want to use something like this instead:
var i = 1; // set your counter to 1
function myLoop() { // create a loop function setTimeout(function() { // call a 3s setTimeout when the loop is called console.log('hello'); // your code here i++; // increment the counter if (i < 10) { // if the counter < 10, call the loop function myLoop(); // .. again which will trigger another } // .. setTimeout() }, 3000)}
myLoop(); // start the loop
Flutter - How to delay a function for some seconds
You'll have to add an helper variable in the outer scope, that will indicate whether the user is on an answer cooldown or not.
The shortest solution will be:
var answerCooldownInProgress = false;
tappedbutton(int index) async {
// Ignore user taps when cooldown is ongoing
if (answerCooldownInProgress) {
return;
}
final userAnswer = await userAnswer();
if (userAnswer) {
// ...
} else {
ErrorSnackbar();
answerCooldownInProgress = true;
await Future.delayed(const Duration(seconds: 2));
answerCooldownInProgress = false;
}
}
Delay-execute any existing function by a call of the form function_name.callAfter(ms, param1, param2, ...)
You can do it by adding a method to the function constructor's prototype object. That way any created function can inherit that method. It's referred to as prototypal inheritance:
Function.prototype.callAfter = function(delay, ...args) {
setTimeout(() => this(...args), delay)
};
Related Topics
What Does the @ Mean Inside an Import Path
Print Content of JavaScript Object
What's the Best Way to Break from Nested Loops in JavaScript
How to Get Elements with Multiple Classes
How to Destructure Object Properties with Key Names That Are Invalid Variable Names
Onclick Function Runs Automatically
Angularjs 1.2 $Injector:Modulerr
Checking If Jquery Is Loaded Using JavaScript
How to Post Form Data with Fetch API
Unexpected Behavior Using Array Map on an Array Initialized with Array Fill
How to Remove All the Options of a Select Box and Then Add One Option and Select It with Jquery
Download and Open PDF File Using Ajax
How to Get Selected HTML Text with JavaScript
How to Add Conditional Attribute in Angular 2
What Is the Cleanest Way to Get the Progress of Jquery Ajax Request