Skip to content

Instantly share code, notes, and snippets.

@dblodorn
Last active March 22, 2017 20:56
Show Gist options
  • Save dblodorn/e97c15fda61450a183c8427f75542342 to your computer and use it in GitHub Desktop.
Save dblodorn/e97c15fda61450a183c8427f75542342 to your computer and use it in GitHub Desktop.
repeat function execution per dom element - doesn't work when more than one selector is in the same view.
// Takes a Selector and Function as Argument
// Supply as Array
export const elementIterate = (config) => {
let fnCount = 0,
i = 0
const iterateFuncShell = (selector,fn) => {
const itemToIterate = document.querySelectorAll(selector)
setTimeout(() => {
if(itemToIterate) {
while(i < itemToIterate.length) {
fn(itemToIterate,i)
i++
}
}
}, 5)
}
while (fnCount < config.length) {
iterateFuncShell(config[fnCount].selector,config[fnCount].fn)
fnCount++
}
}
import {elementIterate} from './element-iterate'
// Iterate Functions - Examples.
const functionOne = (itemToIterate,i) => {
console.log(itemToIterate[i])
}
const functionTwo = (itemToIterate,i) => {
const str = itemToIterate[i].innerHTML
itemToIterate[i].innerHTML = str.split(' – ', 2)[0] + '<br>' + str.split(' – ', 2)[1]
}
// Api for elementiterate function shell
elementIterate ([
{
selector: '.some-selector',
fn: functionOne
},
{
selector: '.some-other-selector',
fn: functionTwo
},
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment