Update script.js
- Fetch for new postings every 10 seconds
This commit is contained in:
parent
4394b6d2f3
commit
ef4b7ed9f5
1 changed files with 48 additions and 46 deletions
50
script.js
50
script.js
|
@ -1,8 +1,7 @@
|
|||
$(document).ready(function() {
|
||||
let existingPosts = [];
|
||||
let existingPosts = [];
|
||||
|
||||
// Function to calculate relative time
|
||||
const timeAgo = function(date) {
|
||||
// Function to calculate relative time
|
||||
const timeAgo = function(date) {
|
||||
let seconds = Math.floor((new Date() - date) / 1000);
|
||||
let interval = seconds / 31536000;
|
||||
if (interval > 1) {
|
||||
|
@ -25,22 +24,15 @@ $(document).ready(function() {
|
|||
return Math.floor(interval) + " minutes ago";
|
||||
}
|
||||
return Math.floor(seconds) + " seconds ago";
|
||||
}
|
||||
}
|
||||
|
||||
// Function to fetch posts for a given hashtag
|
||||
const getPosts = function(hashtag) {
|
||||
// Function to fetch posts for a given hashtag
|
||||
const getPosts = function(hashtag) {
|
||||
return $.get(`https://openbiblio.social/api/v1/timelines/tag/${hashtag}`);
|
||||
}
|
||||
|
||||
// Initialize Masonry
|
||||
$('.masonry-grid').masonry({
|
||||
itemSelector: '.col-sm-3',
|
||||
columnWidth: '.col-sm-3',
|
||||
percentPosition: true,
|
||||
transitionDuration: '0.2s'
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
// Function to fetch and display posts
|
||||
const fetchAndDisplayPosts = function() {
|
||||
// Fetch posts for each hashtag
|
||||
$.when(getPosts('bibliocon23'), getPosts('111bibliocon'), getPosts('bibliocon')).then(function(bibliocon23Posts, bibliocon111Posts, biblioconPosts) {
|
||||
let allPosts = bibliocon23Posts[0].concat(bibliocon111Posts[0], biblioconPosts[0]);
|
||||
|
@ -56,8 +48,8 @@ $(document).ready(function() {
|
|||
existingPosts.push(post.id);
|
||||
|
||||
let cardHTML = `
|
||||
<div class="col-sm-3 mb-2">
|
||||
<div class="card p-2">
|
||||
<div class="col-sm-3">
|
||||
<div class="card m-2 p-2">
|
||||
<div class="d-flex align-items-center mb-2">
|
||||
<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>
|
||||
|
@ -69,10 +61,6 @@ $(document).ready(function() {
|
|||
</div>
|
||||
`;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// Convert the HTML string into a jQuery object
|
||||
let $card = $(cardHTML);
|
||||
|
||||
|
@ -83,6 +71,20 @@ $(document).ready(function() {
|
|||
$('.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);
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue