Skip to content

Instantly share code, notes, and snippets.

@henriklied
Created August 27, 2009 11:10
Show Gist options
  • Save henriklied/176234 to your computer and use it in GitHub Desktop.
Save henriklied/176234 to your computer and use it in GitHub Desktop.
var player;
// Create a unique string for the current viewer,
// that way, we can identify him in the backend later on.
var user_id = new UUID();
// Sleep function in JavaScript
function sleep(milliseconds) {
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds){
break;
}
}
}
function playerReady(obj) {
player = document.getElementById(obj['id']);
};
function getPlaybackPosition(obj) {
xhr = new FlashXMLHttpRequest(); // Use FlashXMLHttpRequest for crossdomain ajax
var url = 'http://myserver.com/stats?video=1273&time='+obj.position+'&uuid='+user_id;
xhr.setRequestHeader("Content-Type", "text/xml");
xhr.open('GET', p);
xhr.send("");
}
function runGetPlayBackPosition() {
player.addModelListener("TIME", "getPlaybackPosition");
// ModelEvent.TIME fires every 100ms. We only want it to fire once,
// so we add the removeModelListener to the function. We need to sleep 100ms first.
// That way, we're certain that the getPlaybackPosition is fired.
sleep(100);
// Remove the model listener.
player.removeModelListener("TIME", "getPlaybackPosition");
}
// Run the script every five seconds (5000ms)
setInterval("runGetPlayBackPosition()", 5000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment