Index: src/wp-includes/js/heartbeat.js
===================================================================
--- src/wp-includes/js/heartbeat.js	(revision 25052)
+++ src/wp-includes/js/heartbeat.js	(working copy)
@@ -26,12 +26,14 @@
 window.wp = window.wp || {};
 
 (function($){
+	var $document = $(document);
+
 	var Heartbeat = function() {
 		var self = this,
 			running,
 			beat,
-			screenId = typeof pagenow != 'undefined' ? pagenow : '',
-			url = typeof ajaxurl != 'undefined' ? ajaxurl : '',
+			screenId = typeof window.pagenow != 'undefined' ? window.pagenow : '',
+			url = typeof window.ajaxurl != 'undefined' ? window.ajaxurl : '',
 			settings,
 			tick = 0,
 			queue = {},
@@ -82,13 +84,6 @@
 			$.extend( this, settings );
 		}
 
-		function time(s) {
-			if ( s )
-				return parseInt( (new Date()).getTime() / 1000 );
-
-			return (new Date()).getTime();
-		}
-
 		function isLocalFrame( frame ) {
 			var origin, src = frame.src;
 
@@ -134,25 +129,25 @@
 
 				if ( trigger && ! self.hasConnectionError() ) {
 					hasConnectionError = true;
-					$(document).trigger( 'heartbeat-connection-lost', [error] );
+					$document.trigger( 'heartbeat-connection-lost', [error] );
 				}
 			} else if ( self.hasConnectionError() ) {
 				errorcount = 0;
 				hasConnectionError = false;
-				$(document).trigger( 'heartbeat-connection-restored' );
+				$document.trigger( 'heartbeat-connection-restored' );
 			}
 		}
 
 		function connect() {
 			var send = {}, data, i, empty = true,
 			nonce = typeof window.heartbeatSettings == 'object' ? window.heartbeatSettings.nonce : '';
-			tick = time();
+			tick = Date.now();
 
 			data = $.extend( {}, queue );
 			// Clear the data queue, anything added after this point will be send on the next tick
 			queue = {};
 
-			$(document).trigger( 'heartbeat-send', [data] );
+			$document.trigger( 'heartbeat-send', [data] );
 
 			for ( i in data ) {
 				if ( data.hasOwnProperty( i ) ) {
@@ -194,7 +189,7 @@
 					errorstate();
 
 				if ( response.nonces_expired ) {
-					$(document).trigger( 'heartbeat-nonces-expired' );
+					$document.trigger( 'heartbeat-nonces-expired' );
 					return;
 				}
 
@@ -219,7 +214,7 @@
 		}
 
 		function next() {
-			var delta = time() - tick, t = interval;
+			var delta = Date.now() - tick, t = interval;
 
 			if ( ! running )
 				return;
@@ -237,12 +232,12 @@
 				beat = window.setTimeout(
 					function(){
 						if ( running )
-							connect();
+							self.connect();
 					},
 					t - delta
 				);
 			} else {
-				connect();
+				self.connect();
 			}
 		}
 
@@ -259,7 +254,7 @@
 			window.clearTimeout(frameBlurTimeout);
 			winBlurTimeout = frameBlurTimeout = 0;
 
-			isUserActive = time();
+			isUserActive = Date.now();
 
 			if ( hasFocus )
 				return;
@@ -281,18 +276,18 @@
 
 				$.data( frame, 'wp-heartbeat-focus', 1 );
 
-				$( frame.contentWindow ).on( 'focus.wp-heartbeat-focus', function(e) {
-					focused();
-				}).on('blur.wp-heartbeat-focus', function(e) {
-					setFrameEvents();
-					frameBlurTimeout = window.setTimeout( function(){ blurred(); }, 500 );
-				});
+				$( frame.contentWindow )
+					.on( 'focus.wp-heartbeat-focus', focused )
+					.on( 'blur.wp-heartbeat-focus', function() {
+						setFrameEvents();
+						frameBlurTimeout = window.setTimeout( blurred, 500 );
+					});
 			});
 		}
 
-		$(window).on( 'blur.wp-heartbeat-focus', function(e) {
+		$(window).on( 'blur.wp-heartbeat-focus', function() {
 			setFrameEvents();
-			winBlurTimeout = window.setTimeout( function(){ blurred(); }, 500 );
+			winBlurTimeout = window.setTimeout( blurred, 500 );
 		}).on( 'focus.wp-heartbeat-focus', function() {
 			$('iframe').each( function( i, frame ) {
 				if ( !isLocalFrame( frame ) )
@@ -307,7 +302,7 @@
 
 		function userIsActive() {
 			userActiveEvents = false;
-			$(document).off( '.wp-heartbeat-active' );
+			$document.off( '.wp-heartbeat-active' );
 			$('iframe').each( function( i, frame ) {
 				if ( ! isLocalFrame( frame ) )
 					return;
@@ -321,20 +316,20 @@
 		// Set 'hasFocus = true' if user is active and the window is in the background.
 		// Set 'hasFocus = false' if the user has been inactive (no mouse or keyboard activity) for 5 min. even when the window has focus.
 		function checkUserActive() {
-			var lastActive = isUserActive ? time() - isUserActive : 0;
+			var lastActive = isUserActive ? Date.now() - isUserActive : 0;
 
 			// Throttle down when no mouse or keyboard activity for 5 min
 			if ( lastActive > 300000 && hasFocus )
-				 blurred();
+				blurred();
 
 			if ( ! userActiveEvents ) {
-				$(document).on( 'mouseover.wp-heartbeat-active keyup.wp-heartbeat-active', function(){ userIsActive(); } );
+				$document.on( 'mouseover.wp-heartbeat-active keyup.wp-heartbeat-active', userIsActive );
 
 				$('iframe').each( function( i, frame ) {
 					if ( ! isLocalFrame( frame ) )
 						return;
 
-					$( frame.contentWindow ).on( 'mouseover.wp-heartbeat-active keyup.wp-heartbeat-active', function(){ userIsActive(); } );
+					$( frame.contentWindow ).on( 'mouseover.wp-heartbeat-active keyup.wp-heartbeat-active', userIsActive );
 				});
 
 				userActiveEvents = true;
@@ -342,11 +337,11 @@
 		}
 
 		// Check for user activity every 30 seconds.
-		window.setInterval( function(){ checkUserActive(); }, 30000 );
-		$(document).ready( function() {
+		window.setInterval( checkUserActive, 30000 );
+		$document.ready( function() {
 			// Start one tick (15 sec) after DOM ready
 			running = true;
-			tick = time();
+			tick = Date.now();
 			next();
 		});
 
@@ -383,7 +378,6 @@
 						// Allow long polling, (experimental)
 						interval = 0;
 						return 0;
-						break;
 					default:
 						seconds = 15;
 						countdown = 0;
@@ -449,13 +443,13 @@
 
 	$.extend( Heartbeat.prototype, {
 		tick: function( data, textStatus, jqXHR ) {
-			$(document).trigger( 'heartbeat-tick', [data, textStatus, jqXHR] );
+			$document.trigger( 'heartbeat-tick', [data, textStatus, jqXHR] );
 		},
 		error: function( jqXHR, textStatus, error ) {
-			$(document).trigger( 'heartbeat-error', [jqXHR, textStatus, error] );
+			$document.trigger( 'heartbeat-error', [jqXHR, textStatus, error] );
 		}
 	});
 
-	wp.heartbeat = new Heartbeat();
+	window.wp.heartbeat = new Heartbeat();
 
 }(jQuery));
