sliding window counter for JavaScript