0

I dived into d3 force & d3.time recently. There are many examples using d3.time with forces together.

I understood d3.interval - which makes d3.timer loop run with comparaly slower pace than as it is.

However, I don't know why this code put d3.now() in the d3.interval. And don't know why there are two elements going on wihtin d3.interval one is 2000, and the other one is d3.now() .

Entier Code: https://jsfiddle.net/onw8v29g/

The point I got stuck.

d3.interval(function() {
  nodes.push(c); // Re-add c.
  links.push({source: b, target: c}); // Re-add b-c.
  links.push({source: c, target: a}); // Re-add c-a.
  restart();
}, 2000, d3.now() + 1000);

P.S I think the most daunting part while learning d3 is decoding the documentation. I recently started reading 'd3 indepth' which is very helpful to decrypt the official documentation. If you guys have other recommendations for understanding d3,please let me know as well.

Thank you.

BS100
  • 823
  • 6
  • 22

1 Answers1

3

d3.interval(callback[, delay[, time]])

Schedules a new timer, invoking the specified callback repeatedly until the interval is stopped. An optional numeric delay in milliseconds may be specified to invoke the given callback after a delay; if delay is not specified, it defaults to zero. The delay is relative to the specified time in milliseconds; if time is not specified, it defaults to now.

So basically the callback actually is called at the clock "( d3.now() + 1000 ) + 2000" ms.

Reference: https://github.com/d3/d3-timer#interval check timer explanation to have an idea of the params specs.

Luchux
  • 803
  • 1
  • 7
  • 17
  • Thanks, I already read the reference. so what does this ' [, ' mean? I have seen this so many times in the document but have no clue about this symbols. If it's like, d3.interval(callback, [delay] , [time]), I could get what it means but I'm not sure the symbols mean the same thing as I guess. – BS100 Apr 03 '20 at 02:55
  • and my main question is why do I need to add d3.now for the functionality... why do i need" d3.now()" specifically? – BS100 Apr 03 '20 at 02:58
  • @BS100 Using square brackets is the typical notation for optional parameters in many programming languages' docs. See also: [*"What do the brackets around the arguments mean when reading documentation for a method?"*](/q/21654192) – altocumulus Apr 03 '20 at 09:35
  • 1
    @BS100 you don´t need it at all. Thats the base of the delay, so if you pass nothing as a 3rd param ([param] is optional), the default value will be Date.now(). Resuming , if its passed the 3rd param, the delay will be summed to this 3rd param (the base time), if its not passed the delay will be from Date.now() so expect a **delay** only in the time interval execution. – Luchux Apr 04 '20 at 06:27