Skip to content

Commit

Permalink
use requestAnimationFrame instead of setInterval for better performance
Browse files Browse the repository at this point in the history
  • Loading branch information
cythilya committed Jul 22, 2018
1 parent 0a251ff commit a0e5c32
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var gulp = require('gulp');
var uglify = require('gulp-uglify');
var uglify = require('gulp-uglify-es').default;
var sass = require('gulp-sass');
var cleanCss = require('gulp-clean-css');
var gutil = require('gulp-util');
Expand Down
23 changes: 17 additions & 6 deletions js/jquery.newsticker.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,37 @@
},
suspend: function() { //suspend newsticker
$newsticker.on('mouseover mouseout', function(e) {
stop = e.type === 'mouseover' ? true : false;
stop = e.type === 'mouseover';
});
},
move: function() { //activate newsticker
if ($.isFunction(config.move)) {
config.move.call(this);
} else {
setInterval(function() {
if (!stop) {
var $current = $frame.find('.current');
let start = null;

function tick(timestamp) {
let progress = timestamp - start;
if (start === null) { start = timestamp; }

if (progress < config.interval || stop) {
window.requestAnimationFrame(tick);
} else {
let $current = $frame.find('.current');

$frame.animate({
top: '-=' + config.height + 'px'
}, config.speed, function() {
}, config.speed, () => {
$next = $frame.find('.current').next().addClass('current');
$current.removeClass('current').clone().appendTo($frame).remove();
$frame.css('top', startPos + 'px');
});
start = timestamp;
window.requestAnimationFrame(tick);
}
}, config.interval);
}

requestAnimationFrame(tick);
}
},
init: function(ticker, height) { //init settings
Expand Down
2 changes: 1 addition & 1 deletion js/jquery.newsticker.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"gulp-debug": "^3.1.0",
"gulp-rename": "^1.2.2",
"gulp-sass": "^2.3.2",
"gulp-uglify": "^2.0.0",
"gulp-uglify-es": "^1.0.4",
"gulp-util": "^3.0.7"
}
}

0 comments on commit a0e5c32

Please sign in to comment.