Last active
February 24, 2017 18:23
-
-
Save davidlwatsonjr/6734d9b16fe8e8db1402bd3835bcaa8c to your computer and use it in GitHub Desktop.
This file contains 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
var loopThroughSongsAnd = function(doThis, interval, runMax) { | |
console.time('Looping through songs'); | |
var getIntOrDefault = function(variable, defaultValue) { | |
variable = parseInt(variable, 10); | |
if (isNaN(variable) || !variable) { | |
variable = getIntOrDefault(defaultValue, 0); | |
} | |
return variable; | |
} | |
interval = getIntOrDefault(interval, 1); | |
runMax = getIntOrDefault(runMax, 1000); | |
var mainPanel = document.querySelector("#mainContainer"); | |
var mainPanelRectangle = mainPanel.getClientRects()[0]; | |
mainPanel.scrollTop = 0; | |
var runCount = 0; | |
var song; | |
var loopInterval = window.setInterval(function() { | |
if (!song || !song.nextElementSibling) { | |
song = document.querySelector('.song-table tr.song-row'); | |
if (!song.nextElementSibling) { | |
console.warn('Could not find next song after ', song); | |
stopLooping(); | |
} | |
} | |
var songRectangle = song.getClientRects()[0]; | |
if (songRectangle.bottom >= mainPanelRectangle.bottom) { | |
mainPanel.scrollTop = Math.min( | |
mainPanel.scrollTop + songRectangle.top - mainPanelRectangle.top, | |
mainPanel.scrollHeight - mainPanel.clientHeight | |
); | |
} | |
if (!song.nextElementSibling.classList.contains('song-row')) { | |
console.log('Reached last song'); | |
stopLooping(); | |
} | |
if (!doThis(song)) { | |
stopLooping(); | |
} else { | |
song = song.nextElementSibling; | |
} | |
runCount++; | |
if (runCount >= runMax) { | |
console.error('Reached run count maximum of', runMax); | |
stopLooping(); | |
} | |
}, interval); | |
var stopLooping = function() { | |
window.clearInterval(loopInterval); | |
console.timeEnd('Looping through songs'); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment