d3.timer() frame - slowing down - javascript

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);

Related

How to make a js function run immediately and then on a 10sec interval?

It only runs after 10 seconds. I want it to run when the page loads and then on a 10 seconds interval. Hope someone can help me.
function getPrice(){
$("#ajax").load('somefile.php?sym=<?php echo $yahoosymbol;?> #ajax');
}
getPrice();
setTimeout(getPrice, 10000);
UPDATE:
I got it to work after putting the function into a <body onload="function()">
none of the other things worked.. I still wonder why?
Try this :
function getPrice(){
$("#ajax").load('your code...');
}
getPrice(); //for initial execution
setInterval(getPrice, 10000); //runs the function on 10sec. interval
The setTimeout() method calls a function or evaluates an expression after a specified number of milliseconds
whereas setInterval() method does the same thing but at specified intervals repeatedly

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.

Can you perform an action at 5 seconds into playing a sound file using Sound.js?

I have my sounds created and playing my html5 page ok on all devices.
I have some animations that I want to reveal when the audio reaches certain points.
maybe at 5, 10 , 25 seconds.
Is that possible if so can you provide sample code to call a function at a certain time interval?
You could achieve this very simply using setTimeout():
// Set up functions that will be triggered during sound playback...
var a = function(){
console.log("Do something after 5 seconds");
}
var b = function(){
console.log("Do something after 10 seconds");
}
var c = function(){
console.log("Do something after 25 seconds");
}
// Play the sound...
createjs.Sound.play("page1");
// Immediately after playing the sound, trigger the time out functions...
setTimeout(a, 5000); // Triggers after 5 seconds of playback
setTimeout(b, 10000); // Triggers after 10 seconds of playback
setTimeout(c, 25000); // Triggers after 25 seconds of playback
Working example
More information on setTimeout can be found here: http://javascript.info/tutorial/settimeout-setinterval
setTimeout
The syntax is: var timerId = setTimeout(func|code, delay)
func|code – Function variable or the string of code to execute.
delay – The delay in microseconds, 1000 microseconds = 1 second. The execution
will occur after the given delay.

Javascript are these calls the same in Node.js?

I'm wondering if these two blocks of code are the same in Node.js?
// Style 1
setTimeout(function () {
console.log('hello');
}, 0);
// Style 2
console.log('hello');
Since above I'm passing 0 for the timeout, there should be no waiting time. Is it identical to just calling console.log('hello'); directly without using setTimeout?
They are different, the first adds the function to the event queue, so that it can execute as soon as it gets a chance after the current execution path completes. The second will execute it immediately.
For example:
console.log('first');
setTimeout(function(){
console.log('third');
}, 0);
console.log('second');
The order that those are printed in is well defined, you could even do something slow (but synchronous) before printing 'second'. It's guaranteed that console.log('second'); will still execute before the callback to setTimeout does:
console.log('first');
setTimeout(function () {
console.log('third'); // Prints after 8 seconds
}, 0);
// Spinlock for 3 seconds
(function(start){ while(new Date - start < 3000); })(new Date);
console.log('second'); // Prints after 3 seconds, but still before 'third'
// Spinlock for 5 seconds
(function(start){ while(new Date - start < 5000); })(new Date);
Strictly speaking they are not exactly the same - setTimeout gives the browser a chance to "catch up" with any tasks that it desperately needs to do. But as far as we're normally concerned, 99% of the time they will do the same thing.

how can I execute a javascript program 30 after page loads

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 :)

Categories