Fork Copy (function () { function simpleLoop(startTime, endTime) { // TODO: Check valid endtime console.log('-- Run here') var ytplayer = document.getElementById("movie_player"); var currentTime = ytplayer.getCurrentTime(); var isRunToStart = currentTime > startTime var suitableTimeout = endTime - startTime if (currentTime <= startTime || currentTime > endTime) { ytplayer.seekTo(startTime) } else { suitableTimeout = endTime - currentTime } console.log('>> Start timeout after: ', suitableTimeout * 1000) setTimeout(function () { simpleLoop(startTime, endTime) }, suitableTimeout * 1000) } var savedTimerange = localStorage.getItem('simple_loop_' + location.href); var input = prompt('Time range: start,end', savedTimerange || ''); if (!input) { return; } localStorage.setItem('simple_loop_' + location.href, input); var arr = input.split(','); simpleLoop(parseInt(arr[0]), parseInt(arr[1])); })();