I have little problem with my function fetch with api. I have variable currentPage outside function popular(), in that case everything works, but when Im trying declare inside function popular() that variable to get increment, then my number is still 1. Is there any way to get increment without declaring a global variable but with use local? After pressing the button (nextPage ()) the number of currentPage ++ should increase each time.
let currentPage = 1; <--- //global variable(I dont want)
popular();
function popular(page) {
fetch("https://api.themoviedb.org/3/movie/popular?api_key=xxxxxxxxxxxxxxxxxxx&page="+ page+"")
.then(resp => resp.json())
.then(resp => {
movieApi = resp;
movieResult = resp.results;
maxLenght = resp.total_pages;
document.getElementById("movie-section").innerHTML = `${movieResult.map(movieCard).join("")}`;
document.getElementById("btns-container").innerHTML = paginationCont();
shortText();
function paginationPage(){
console.log("maxLenght " +maxLenght);
const btnNext = document.getElementById("nextBtn");
const btnPrev = document.getElementById("prevBtn");
function prevPage() {
if (currentPage > 1) {
currentPage--;
popular(currentPage);
}
}
function nextPage() {
if (currentPage < maxLenght) {
currentPage++; <-- //it does not work when it declares inside
popular(currentPage);
}
}
btnNext.addEventListener("click", nextPage);
btnPrev.addEventListener("click", prevPage);
}
paginationPage();
});
}