Last active
March 22, 2017 20:56
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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++ | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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