how can I execute a javascript program 30 after page loads - javascript

how can I execute a javascript program 30 after page loads, and I have to execute it repeatedly in 30 seconds. I would like to use jquery since I already included in my document
Thank you guys

You could use the window.setTimeout method:
window.setTimeout(function() {
// This will execute 30s after the page loads
// and it will execute only once
}, 30000);
If you want to repeat the execution of the function on every 30 seconds you could use the setInterval method.
As far as jquery is concerned you don't need to to use it for such a simple task as it is already built into javascript.

You want setInterval() here, like this:
setInterval(function() {
alert("It's been 30 seconds");
}, 30000);
setInterval() will fire after the delay (so 30 seconds in) then again every time the delay is up (every 30 seconds as desired).

If you want to execute it 30 times, once a second, you'd use setInterval.
var interval = window.setInterval(function() {
// do stuff
}, 1000);
window.setTimeout(function() {
window.clearInterval(interval);
}, 30 * 1000);

$.ajaxSetup({
cache: false
});
setInterval(function() {
$("#data1").load('1.php');
}, 30000);
});
Just substitute your jQuery function in :)

Related

d3.timer() frame - slowing down

is it possible to slow down frames in
d3.timer(function(){ alert("Hello"); }, 3000); ?
Here we have delay of 3 sec and then it becomes very fast.
For example in
setInterval(function(){ alert("Hello"); }, 3000);
we are able to change 3000 milliseconds to 2000 and we will be continuously alert every 2 sec instead of 3.
Thank you very much!
API reference of D3-timer says:
d3.interval(callback[, delay[, time]]) <>
Like timer, except the callback is invoked only every delay
milliseconds; if delay is not specified, this is equivalent to timer.
A suitable replacement for setInterval that is guaranteed to not run
in the background. The callback is passed the elapsed time.
You can find it here: github
In your case:
d3.interval(function(){ alert("Hello") }, 3000);

Delay first function call for n-seconds [duplicate]

This question already has answers here:
Why is the method executed immediately when I use setTimeout?
(8 answers)
Closed 6 years ago.
I'm having a javascript function which I have set an interval to execute every 30 seconds. This is my code:
function ping() {
// code ...
}
ping(); // call function when page loads for first time
// call function every 30 seconds
window.setInterval(function(){
ping();
}, 30000);
What I'd like to do is to delay the first call (right when the page loads for first time) for 5 seconds and then the function should execute again every 30 secs.
I have tried the setTimeout, but doesn't seem to work, it executes imidiatelly any ideas what I'm doing wrong ?
setTimeout(ping(), 5000); // delay first call for 5 secs
// call function every 30 seconds
window.setInterval(function(){
ping();
}, 30000);
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);
You are calling it immediately on setTimeout(ping(), 5000);. You are setting the result of ping() as the timeout function. If you want ping to be called by the timeout, then either do setTimeout(ping, 5000); or wrap it in a closure, like you did the second time.

Javascript setTimeout function repeat

Is there a way to repeat a function in Javascript using the setTimeout function? For example, I need two functions to be called every five seconds. I have something like this:
$(document).ready(function(){
setTimeout('shiftLeft(); showProducts();', 5000);
});
but it only happens once, five seconds after page loads, and I need it to happen every five seconds.
Use setInterval() instead of setTimeout(), if you want your function to be executed repeatedly. setTimeout() delays the execution of your function for x seconds, while setInterval() executes your function every x seconds.
Both within the boundaries of the event queue of JavaScript, so don't be too confident, that you functions get executed at the exact time you specified
$(document).ready(function(){
setInterval( function(){ shiftLeft(); showProducts(); }, 5000);
});
Every x seconds can be done with setInterval:
$(document).ready(function(){
setInterval(function(){
shiftLeft(); showProducts();
}, 5000);
});
$(document).ready(function(){
setTimeout(function(){
shiftLeft(); showProducts();
}, 5000);
});

Run a Javascript in timed intervals?

How can I run a Javascript function with per-set time delays, without using any framework?
I have a Ajax script which will fetch the no of online users from server. This script i want to run in a regular timed delays.
Thanks in advance.
The simplest way would be with setInterval:
setInterval(function() {
console.log("run every second");
}, 1000);
This will run the given code at the specified time interval over and over.
var interval = window.setInterval(function() {
console.log("foo");
}, 1000);
And stop
window.clearInterval(interval);
You can use setInterval to do this.
See: https://developer.mozilla.org/en/DOM/window.setInterval
var int = self.setInterval(askQuestion,1000);
function askQuestion() {
// do something... code to be implemented.
}
Will be call askQuestion() every 1000 milliseconds (1 seconds) for example..
<script>
var intervalVariable=setInterval(function(){
//Your operation goes Here
},1000); // executes every 1000 milliseconds(i.e 1 sec)
function stopTimer()
{
clearInterval(intervalVariable); // To clear TimerInterval/stop the setInterval Function
}
</script>

JQuery wait a number of seconds before the next line of code is executed

I need a simple way or pausing a few seconds before the next line of code is executed.
So I have:
$('.myClass').show();
//WAIT FOR 5 SECONDS HERE
$('.myClass').hide();
setTimeout:
$('.myClass').show();
window.setTimeout(function (){$('.myClass').hide(); }, 5000);
$('.myClass').show().delay(5000).hide(); 
Only subsequent events in a queue are delayed; for example this will
not delay the no-arguments forms of .show() or .hide() which do not
use the effects queue.
docs
In order to use delay you have to use duration so it will use the queue:
$('.myClass').show().delay(5000).hide(0);
JSFiddle DEMO
Thanks #am not i am! (again...)
This should work:
$('.myClass').show();
window.setTimeout(
function() {
$('.myClass').hide();//happens 5 secs later
},
5000
);
See window.setTimeout on MDN
You can enclose the hide in a call to setTimeout().
$('.myClass').show();
setTimeout(function() {$('.myClass').hide()}, 5000);
$('.myClass').show();
window.setTimeout(function () {
$('.myClass').hide();
}, 5000);

Categories