Update script.js

- Fetch for new postings every 10 seconds
This commit is contained in:
Ralf Stockmann 2023-05-17 15:46:48 +02:00 committed by GitHub
parent 4394b6d2f3
commit ef4b7ed9f5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,4 +1,3 @@
$(document).ready(function() {
let existingPosts = []; let existingPosts = [];
// Function to calculate relative time // Function to calculate relative time
@ -32,15 +31,8 @@ $(document).ready(function() {
return $.get(`https://openbiblio.social/api/v1/timelines/tag/${hashtag}`); return $.get(`https://openbiblio.social/api/v1/timelines/tag/${hashtag}`);
} }
// Initialize Masonry // Function to fetch and display posts
$('.masonry-grid').masonry({ const fetchAndDisplayPosts = function() {
itemSelector: '.col-sm-3',
columnWidth: '.col-sm-3',
percentPosition: true,
transitionDuration: '0.2s'
});
// Fetch posts for each hashtag // Fetch posts for each hashtag
$.when(getPosts('bibliocon23'), getPosts('111bibliocon'), getPosts('bibliocon')).then(function(bibliocon23Posts, bibliocon111Posts, biblioconPosts) { $.when(getPosts('bibliocon23'), getPosts('111bibliocon'), getPosts('bibliocon')).then(function(bibliocon23Posts, bibliocon111Posts, biblioconPosts) {
let allPosts = bibliocon23Posts[0].concat(bibliocon111Posts[0], biblioconPosts[0]); let allPosts = bibliocon23Posts[0].concat(bibliocon111Posts[0], biblioconPosts[0]);
@ -56,8 +48,8 @@ $(document).ready(function() {
existingPosts.push(post.id); existingPosts.push(post.id);
let cardHTML = ` let cardHTML = `
<div class="col-sm-3 mb-2"> <div class="col-sm-3">
<div class="card p-2"> <div class="card m-2 p-2">
<div class="d-flex align-items-center mb-2"> <div class="d-flex align-items-center mb-2">
<img src="${post.account.avatar}" class="rounded-circle mr-2" width="50" height="50"> <img src="${post.account.avatar}" class="rounded-circle mr-2" width="50" height="50">
<h5 class="card-title m-0"><a href="${post.account.url}" target="_blank">${post.account.username}</a></h5> <h5 class="card-title m-0"><a href="${post.account.url}" target="_blank">${post.account.username}</a></h5>
@ -69,10 +61,6 @@ $(document).ready(function() {
</div> </div>
`; `;
// Convert the HTML string into a jQuery object // Convert the HTML string into a jQuery object
let $card = $(cardHTML); let $card = $(cardHTML);
@ -83,6 +71,20 @@ $(document).ready(function() {
$('.masonry-grid').masonry('prepended', $card); $('.masonry-grid').masonry('prepended', $card);
} }
}); });
});
};
$(document).ready(function() {
// Initialize Masonry
$('.masonry-grid').masonry({
itemSelector: '.col-sm-3',
columnWidth: '.col-sm-3',
percentPosition: true
}); });
// Fetch posts for each hashtag on page load
fetchAndDisplayPosts();
// Fetch posts for each hashtag every 10 seconds
setInterval(fetchAndDisplayPosts, 10000);
}); });