Skip to content

Instantly share code, notes, and snippets.

@sahin
Last active March 10, 2017 13:19
Show Gist options
  • Save sahin/e7c627ba83547e44bf76 to your computer and use it in GitHub Desktop.
Save sahin/e7c627ba83547e44bf76 to your computer and use it in GitHub Desktop.
//Load jQuery library using plain JavaScript
(function(){
var newscript = document.createElement('script');
newscript.type = 'text/javascript';
newscript.async = true;
newscript.src = 'https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js';
(document.getElementsByTagName('head')[0]||document.getElementsByTagName('body')[0]).appendChild(newscript);
})();
function next_page_or_close(selector){
next = $(selector);
if(next.length > 0){
window.location = next.attr('href')
}
else{
window.close();
}
}
function sleep(milliseconds) {
return new Promise(function (resolve, reject) {
setTimeout(resolve, milliseconds);
});
}
function asyncForEach(array, func) {
return new Promise(function (resolve, reject) {
var i = 0;
var next = function () {
i++;
if (i >= array.length) {
resolve();
return;
}
func(array[i], i, next);
};
func(array[i], i, next);
});
}
@AlicanC
Copy link

AlicanC commented Mar 10, 2017

const divs = /* ... */

asyncForEach(divs, (div, index, next) => {
  /* work with div */

  // Wait 1 second
  sleep(1000)
    // Proceed to next div
    .then(next);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment