From 928cf05c57bcedac6c83e8c22be79793ad3ce814 Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Fri, 24 Mar 2017 20:37:23 -0700 Subject: [PATCH] Ensure we use mw.now() instead of 'new Date' for relative measurement 'new Date' is subject to clock drift etc. mw.now() uses performance.now() when available, which will always increase at a constant rate independent of the system clock. Change-Id: Ib653103bf6116544f35c930fb33421f1bb362c7d --- resources/src/jquery/jquery.suggestions.js | 8 ++++---- resources/src/mediawiki/mediawiki.Upload.BookletLayout.js | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/resources/src/jquery/jquery.suggestions.js b/resources/src/jquery/jquery.suggestions.js index fdc8a266ad..75f1ba6e8e 100644 --- a/resources/src/jquery/jquery.suggestions.js +++ b/resources/src/jquery/jquery.suggestions.js @@ -96,7 +96,7 @@ */ // jscs:enable checkParamNames -( function ( $ ) { +( function ( $, mw ) { var hasOwn = Object.hasOwnProperty; @@ -178,7 +178,7 @@ context.data.prevText = val; // Try cache first if ( context.config.cache && hasOwn.call( cache, val ) ) { - if ( +new Date() - cache[ val ].timestamp < context.config.cacheMaxAge ) { + if ( mw.now() - cache[ val ].timestamp < context.config.cacheMaxAge ) { context.data.$textbox.suggestions( 'suggestions', cache[ val ].suggestions ); if ( typeof context.config.update.after === 'function' ) { context.config.update.after.call( context.data.$textbox, cache[ val ].metadata ); @@ -203,7 +203,7 @@ cache[ val ] = { suggestions: suggestions, metadata: metadata, - timestamp: +new Date() + timestamp: mw.now() }; } }, @@ -786,4 +786,4 @@ * @mixins jQuery.plugin.suggestions */ -}( jQuery ) ); +}( jQuery, mediaWiki ) ); diff --git a/resources/src/mediawiki/mediawiki.Upload.BookletLayout.js b/resources/src/mediawiki/mediawiki.Upload.BookletLayout.js index 172cac2379..2f90fe6847 100644 --- a/resources/src/mediawiki/mediawiki.Upload.BookletLayout.js +++ b/resources/src/mediawiki/mediawiki.Upload.BookletLayout.js @@ -230,7 +230,7 @@ */ mw.Upload.BookletLayout.prototype.uploadFile = function () { var deferred = $.Deferred(), - startTime = new Date(), + startTime = mw.now(), layout = this, file = this.getFile(); @@ -265,7 +265,7 @@ deferred.reject( errorMessage ); } ); }, function ( progress ) { - var elapsedTime = new Date() - startTime, + var elapsedTime = mw.now() - startTime, estimatedTotalTime = ( 1 / progress ) * elapsedTime, estimatedRemainingTime = moment.duration( estimatedTotalTime - elapsedTime ); layout.emit( 'fileUploadProgress', progress, estimatedRemainingTime ); -- 2.20.1