Skip to content

Instantly share code, notes, and snippets.

@dpmccabe
Last active April 11, 2016 13:06
Show Gist options
  • Save dpmccabe/5456279 to your computer and use it in GitHub Desktop.
Save dpmccabe/5456279 to your computer and use it in GitHub Desktop.
class @VideoProgressTracker
constructor: (@video) ->
@played_five_seconds = 0
@checkpoints = [0, 25, 50, 75, 95]
track: ->
@played_five_seconds += 1
@triggerCheckpoints(500 * @played_five_seconds / @duration)
start: ->
@duration ?= @video.getDuration()
clearInterval(@interval)
@setUpdateInterval()
pause: ->
clearInterval(@interval)
setUpdateInterval: ->
callback = => @track()
@interval = setInterval callback, 5000
triggerCheckpoints: (current_percent) ->
if current_percent >= @checkpoints[0]
completed_percent = @checkpoints.shift()
$.post(gon.record_video_progress_path, { lesson_id: gon.lesson_id, percent: completed_percent })
jwplayer("video").setup
events:
onPlay: (e) ->
video_tracker.start()
onPause: (e) ->
video_tracker.pause()
@.video_tracker = new VideoProgressTracker(jwplayer("video"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment