Skip to content

Instantly share code, notes, and snippets.

@cbeer
Created February 28, 2011 18:03
Show Gist options
  • Save cbeer/847721 to your computer and use it in GitHub Desktop.
Save cbeer/847721 to your computer and use it in GitHub Desktop.
(function($) {
$.fn.sync = function(target, options) {
media = this;
var settings = {
'begin' : 'begin_seconds',
'end' : 'end_seconds',
'on' : function() { $(this).trigger('sync-on'); },
'off' : function() { $(this).trigger('sync-off'); },
'time' : function() { return this.currentTime },
'poll' : false,
'pollingInterval' : 1000
}
$.extend( settings, options );
if(settings['poll']) {
setInterval(function() { $(media).trigger('timeupdate'); }, settings['pollInterval']);
}
this.bind('timeupdate', function() {
t = jQuery.proxy(settings['time'], this)();
target.each(function() {
if($(this).data(settings['begin']) <= t && t <= $(this).data(settings['end'])) {
if($(this).data('sync') != true) {
$(this).data('sync', true);
jQuery.proxy(settings['on'], this)();
}
} else {
if($(this).data('sync') == true) {
$(this).data('sync', false);
jQuery.proxy(settings['off'], this)();
}
}
});
});
}
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment