npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

each-after

v1.0.14

Published

Iterate through an array with an interval between each call. Stop & kill iteration.

Downloads

33

Readme

each-after

Process each element after n seconds

A library for looping through an array with a delay between each iteration. It was intended for usage with animations, for situations when multiple elements need to have their behaviour stepped. E.g. animating a grid of elements to create a visual pattern. It also offers the ability to immediately stop or kill a current iteration.

Works great with pixi-timeout

Install

npm install each-after

Usage

each-after supports common.js and es6-modules

common.js

const eachAfter = require('each-after')()

es6 modules

import eachAfterTimer from 'each-after'
const eachAfter = eachAfterTimer()

Simple example

Below is the most basic usage

const timerInstance = eachAfter(
    [2,4,9,16],     // an array to iterate over
    1,              // seconds between each iteration
    function(){}    // function to call on each iteration
)

On each

You must pass a function as the 3rd parameter for handling the each iteration of the loop

  • element - The current element being processed in the array.
  • index - The index of the current element being processed in the array
  • processed - The array of all the elements processed so far
  • interval - The time in seconds since the last element was processed
  • wasStopped - Whether the iteration was stoppped manually via .stop()
const onEach = (element, index, processed, interval, wasStopped) = {
    console.log(element, index, processed, interval, wasStopped)

    // Would fire 4 times with the following results
    // 2, 0, [2], 1, false
    // 4, 1, [2,4], 1, false
    // 9, 2, [2,4,9], 1, false
    // 16, 3, [2,4,9,16], 1, false
}

const timerInstance = eachAfter(
    [2,4,9,16],
    1,
    onEach
)

On completion

Optionally you can pass a function as the 4th parameter for handling the completion of the loop

const onComplete = (finalArray, wasStopped) = {
    console.log(finalArray) // [2,4,9,16]
    console.log(wasStopped) // whether the timer was stopped via the `.stop()` method
}

const timerInstance = eachAfter(
    [2,4,9,16],
    1,
    onEach
    onComplete // (Optional) function to call at the end of the iteration
)

Prevent instantly firing first iteration

By default the first iteration is called immediately and then the remaining iterations are delayed, to prevent this behaviour you can pass false as the 5th parameter

const timerInstance = eachAfter(
    [2,4,9,16],
    1,
    onEach
    onComplete
    false,   // (Optional) default = true
)

Methods

Additional methods for manipulating the timer during iteration.

Set interval

The interval can be changed at any time during iteration. Calling timerInstance.setInterval(0) with value of 0 will cause the iterations to happen in the same stack via a standard loop

const timerInstance = eachAfter([2,4,9,16],1,onEach)

timerInstance.setInterval(10) // seconds

Stop

Iteration can be stopped by calling stop and it will trigger all the iterations immediately, including the completionHandler, avoiding using the timer function. This method is essentially the same as timerInstance.setInterval(0). The state of the iteration can be checked via the onEach and onComplete handlers by checking the wasStopped parameter. Stop can only be called before the iteration is completed to prevent multiple onComplete events.

const timerInstance = eachAfter(
    [2,4,9,16],
    1,
    onEach: (ele, idx, pro, int, wasStopped) => { }, // wasStopped will be true
    onComplete: (arr, wasStopped) => { } // wasStopped will be true
)

timerInstance.stop()

Kill

Iteration can be completely killed, which will prevent any remaing handlers, including the completionHandler, from being fired

const timerInstance = eachAfter([2,4,9,16],1,onEach)

timerInstance.kill()

Alternate timeout methods

By default each-after uses setTimeout and clearTimeout to perform the delays. If you would prefer to use your own methds you can pass them on creation.

import eachAfterTimer from 'each-after'

const eachAfter = eachAfterTimer({
    set: (seconds, func) => { /*... your set timeout function */ },
    clear: (timerId) => { /*... your clear timeout function */ },
})

Pixi-Timeout

Here is an example of using custom timeout methods. It uses pixi-timeout requestAnimationFrame based timing functions, which additionally allow for pausing & resuming the timers via the PIXI.Application.stop and PIXI.Application.start methods

import pix from ‘pixi,js’
import pixiTimeout from 'pixi-timeout'
import EachAfterCreate from 'each-after'
window.eachAfter = EachAfterCreate({
    set:PIXI.setTimeout,
    clear:PIXI.clearTimeout
})