0% found this document useful (0 votes)
7 views7 pages

Usajobs Retrieval

The document outlines a JavaScript implementation for fetching and displaying job listings from the USAJobs API. It includes functions for fetching job data, shuffling the results, displaying them in a slideshow format, and navigating through the listings. The slideshow automatically rotates every 5 seconds and allows users to manually navigate using next and previous buttons.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views7 pages

Usajobs Retrieval

The document outlines a JavaScript implementation for fetching and displaying job listings from the USAJobs API. It includes functions for fetching job data, shuffling the results, displaying them in a slideshow format, and navigating through the listings. The slideshow automatically rotates every 5 seconds and allows users to manually navigate using next and previous buttons.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 7

document.

addEventListener('DOMContentLoaded', function () {
const host = 'data.usajobs.gov';
const userAgent = 'clindeman@peregrineadvisors.com';
const authKey = 'XLNeOm1IYZ6JcF34elHhjQgfXeTe12NdkrDxZBOHPGc=';
let jobIndex = 0;

async function fetchJobs() {


try {
const response = await fetch('https://data.usajobs.gov/api/search?
Keyword=Data&ResultsPerPage=100', {
method: 'GET',
headers: {
"Host": host,
"User-Agent": userAgent,
"Authorization-Key": authKey
}
});

if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}

const data = await response.json();


const jobs = data.SearchResult.SearchResultItems;
shuffleArray(jobs);
displayJobs(jobs);
} catch (error) {
console.error('Error fetching jobs:', error);
}
}

function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}

function truncateText(text, maxLength) {


if (text.length > maxLength) {
return text.slice(0, maxLength) + '...';
}
return text;
}

function displayJobs(jobs) {
const jobListings = document.getElementById('jobListings');
jobListings.innerHTML = '';

jobs.forEach(job => {
const jobElement = document.createElement('div');
jobElement.className = 'job-listing';
jobElement.innerHTML = `
<h3>${job.MatchedObjectDescriptor.PositionTitle}</h3>

<p>${job.MatchedObjectDescriptor.PositionLocationDisplay}</p>
<p>$
{truncateText(job.MatchedObjectDescriptor.UserArea.Details.MajorDuties || 'No details
available', 100)}</p>
`;
jobListings.appendChild(jobElement);
});
}

function slideJobs(direction) {
const totalJobs = document.querySelectorAll('.job-listing').length;
const jobWidth = document.querySelector('.job-listing').offsetWidth;
const containerWidth = document.querySelector('.slideshow-
container').offsetWidth;
const visibleJobs = Math.floor(containerWidth / jobWidth);
const maxIndex = totalJobs - visibleJobs;

if (direction === 'next') {


jobIndex = Math.min(jobIndex + 1, maxIndex);
} else {
jobIndex = Math.max(jobIndex - 1, 0);
}

const offset = -jobIndex * jobWidth;


document.getElementById('jobListings').style.transform = `translateX($
{offset}px)`;
}

document.getElementById('nextButton').addEventListener('click', function ()
{
slideJobs('next');
});

document.getElementById('prevButton').addEventListener('click', function ()
{
slideJobs('prev');
});
setInterval(function () {
slideJobs('next');
}, 5000); // Rotate every 5 seconds

fetchJobs();
});

HTML

<style type="text/css">
body {
font-family: Arial, sans-serif;
}

.slideshow-container {
width: 80%;
margin: 0 auto;
overflow: hidden;
position: relative;
max-height: 280px;
/* Limit the height of the slideshow */
display: flex;
align-items: center;
/* Vertically center the content */
}

.job-listings {
display: flex;
transition: transform 0.5s ease-in-out;
width: 100%;
/* Ensure the job listings take the full width */
}

.job-listing {
flex: 0 0 33.3333%;
box-sizing: border-box;
padding: 20px;
max-height: 300px;
/* Ensure each job listing does not exceed the height limit */
overflow: hidden;
/* Hide overflow content */
}

.job-listing:not(:last-child) {
border-right: 1px solid #ddd;
}

.nav-buttons {
width: 100%;
display: flex;
justify-content: space-between;
position: absolute;
top: 50%;
transform: translateY(-50%);
}

.nav-button {
background-color: rgba(0, 0, 0, 0.5);
color: white;
border: none;
padding: 10px;
cursor: pointer;
z-index: 1;
/* Ensure buttons are above the job listings */
}
</style>
<div class="slideshow-container">
<div class="job-listings" id="jobListings">&nbsp;</div>
<div class="nav-buttons">
<button class="nav-button" id="prevButton">❮</button>
<button class="nav-button" id="nextButton">❯</button>
</div>
</div>

————————————————————
JS
document.addEventListener('DOMContentLoaded', function () {
const host = 'data.usajobs.gov';
const userAgent = 'clindeman@peregrineadvisors.com';
const authKey = 'XLNeOm1IYZ6JcF34elHhjQgfXeTe12NdkrDxZBOHPGc';
let jobIndex = 0;

async function fetchJobs() {


try {
const response = await fetch('https://data.usajobs.gov/api/search?
Keyword=Data&ResultsPerPage=100', {
method: 'GET',
headers: {
"Host": host,
"User-Agent": userAgent,
"Authorization-Key": authKey
}
});

if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}

const data = await response.json();


const jobs = data.SearchResult.SearchResultItems;
shuffleArray(jobs);
displayJobs(jobs);
} catch (error) {
console.error('Error fetching jobs:', error);
}
}

function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}

function truncateText(text, maxLength) {


if (text.length > maxLength) {
return text.slice(0, maxLength) + '...';
}
return text;
}

function displayJobs(jobs) {
const jobListings = document.getElementById('jobListings');
jobListings.innerHTML = '';

jobs.forEach(job => {
const jobElement = document.createElement('div');
jobElement.className = 'job-listing';
jobElement.innerHTML = `
<a href="${job.MatchedObjectDescriptor.ApplyURI[0]}" target="_blank"
class="apply-button">Apply Now</a>
<h3>${job.MatchedObjectDescriptor.PositionTitle}</h3>
<p>${job.MatchedObjectDescriptor.PositionLocationDisplay}</p>
<p>$
{truncateText(job.MatchedObjectDescriptor.UserArea.Details.MajorDuties || 'No details
available', 100)}</p>
`;
jobListings.appendChild(jobElement);
});
}

function slideJobs(direction) {
const totalJobs = document.querySelectorAll('.job-listing').length;
const jobWidth = document.querySelector('.job-listing').offsetWidth;
const containerWidth = document.querySelector('.slideshow-
container').offsetWidth;
const visibleJobs = Math.floor(containerWidth / jobWidth);
const maxIndex = totalJobs - visibleJobs;

if (direction === 'next') {


jobIndex = Math.min(jobIndex + 1, maxIndex);
} else {
jobIndex = Math.max(jobIndex - 1, 0);
}

const offset = -jobIndex * jobWidth;


document.getElementById('jobListings').style.transform = `translateX($
{offset}px)`;
}

document.getElementById('nextButton').addEventListener('click', function () {
slideJobs('next');
});

document.getElementById('prevButton').addEventListener('click', function () {
slideJobs('prev');
});
setInterval(function () {
slideJobs('next');
}, 5000); // Rotate every 5 seconds

fetchJobs();
});

<input placeholder="" type="text" class="block flex-auto width-full text-color-default


overflow-hidden break-word pre-wrap placeholder-solid-quieter text-size-default line-
height-4 colors-background-default stroked-blue-inset-outset-focus rounded-big px1
css-1jhs511 shadow-elevation-low shadow-elevation-low-hover-not-focused" value=""
id="631d01d9222bfdb6aeac1abdc21f0874" aria-required="false" style="min-height:
32px; border-radius: 6px; border: 0px;">

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy