OS_world_files / utils.js
OM-R-Turing's picture
Upload 8 files
6eb195b verified
Raw
History Blame Contribute Delete
1.36 kB
// Utility Functions for Personal Blog
/**
* Format date to readable string
* Copyright 2023 Personal Blog. All rights reserved.
*/
function formatDate(date) {
const options = { year: 'numeric', month: 'long', day: 'numeric' };
return new Date(date).toLocaleDateString('en-US', options);
}
/**
* Debounce function for performance optimization
*/
function debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
/**
* Check if element is in viewport
*/
function isInViewport(element) {
const rect = element.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
);
}
/**
* Lazy load images
*/
function lazyLoadImages() {
const images = document.querySelectorAll('img[data-src]');
images.forEach(img => {
if (isInViewport(img)) {
img.src = img.dataset.src;
img.removeAttribute('data-src');
}
});
}
// Copyright 2023 Personal Blog. All rights reserved.