From ef4b7ed9f56c504853da9927494786eb5d48fc11 Mon Sep 17 00:00:00 2001 From: Ralf Stockmann Date: Wed, 17 May 2023 15:46:48 +0200 Subject: [PATCH] Update script.js - Fetch for new postings every 10 seconds --- script.js | 94 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 48 insertions(+), 46 deletions(-) diff --git a/script.js b/script.js index cc9fa16..6329f25 100644 --- a/script.js +++ b/script.js @@ -1,46 +1,38 @@ -$(document).ready(function() { - let existingPosts = []; +let existingPosts = []; - // Function to calculate relative time - const timeAgo = function(date) { - let seconds = Math.floor((new Date() - date) / 1000); - let interval = seconds / 31536000; - if (interval > 1) { - return Math.floor(interval) + " years ago"; - } - interval = seconds / 2592000; - if (interval > 1) { - return Math.floor(interval) + " months ago"; - } - interval = seconds / 86400; - if (interval > 1) { - return Math.floor(interval) + " days ago"; - } - interval = seconds / 3600; - if (interval > 1) { - return Math.floor(interval) + " hours ago"; - } - interval = seconds / 60; - if (interval > 1) { - return Math.floor(interval) + " minutes ago"; - } - return Math.floor(seconds) + " seconds ago"; +// Function to calculate relative time +const timeAgo = function(date) { + let seconds = Math.floor((new Date() - date) / 1000); + let interval = seconds / 31536000; + if (interval > 1) { + return Math.floor(interval) + " years ago"; } - - // Function to fetch posts for a given hashtag - const getPosts = function(hashtag) { - return $.get(`https://openbiblio.social/api/v1/timelines/tag/${hashtag}`); + interval = seconds / 2592000; + if (interval > 1) { + return Math.floor(interval) + " months ago"; } + interval = seconds / 86400; + if (interval > 1) { + return Math.floor(interval) + " days ago"; + } + interval = seconds / 3600; + if (interval > 1) { + return Math.floor(interval) + " hours ago"; + } + interval = seconds / 60; + if (interval > 1) { + return Math.floor(interval) + " minutes ago"; + } + return Math.floor(seconds) + " seconds ago"; +} - // Initialize Masonry - $('.masonry-grid').masonry({ - itemSelector: '.col-sm-3', - columnWidth: '.col-sm-3', - percentPosition: true, - transitionDuration: '0.2s' - }); - +// Function to fetch posts for a given hashtag +const getPosts = function(hashtag) { + return $.get(`https://openbiblio.social/api/v1/timelines/tag/${hashtag}`); +} +// 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 = ` -
-
+
+
${post.account.username}
@@ -67,11 +59,7 @@ $(document).ready(function() {

${timeAgo(new Date(post.created_at))}

- `; - - - - + `; // 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); });