[英]How can I go to the next page with the api?
hello everyone i am new to javascript.大家好,我是 javascript 新手。 I'm having a problem with the API, can you help me?
我的 API 有问题,你能帮我吗? Thank you from now.
从现在开始谢谢你。
api_url is not updating for index. api_url 未更新索引。 Next page 2,3,4 It isn't happening.
下一页 2,3,4 它没有发生。 But accepts 1...
但接受 1...
var index = 1; const main = document.getElementById("main"); const API_URL = "https://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc&api_key=xxxxxxxxxxxxxxxxxxxx&page=" + index; next.addEventListener("click", () => { index = index + 1; getMovies(API_URL); }); prev.addEventListener("click", () => { if (index == 1 || index == 0) { alert("not working"); } else { index = index - 1; getMovies( "https://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc&api_key=xxxxxxxxxxxxxxxxxxxxx&page=" + index ); } });
This is working.这是有效的。 But How does the above work?
但是以上是如何工作的?
var index = 1; const main = document.getElementById("main"); const API_URL = "https://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc&api_key=xxxxxxxxxxxxxxxxxxxx&page=" + index; next.addEventListener("click", () => { index = index + 1; getMovies( "https://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc&api_key=xxxxxxxxxxxxxxxxxxxxx&page=" + index ); }); prev.addEventListener("click", () => { if (index == 1 || index == 0) { alert("not working"); } else { index = index - 1; getMovies( "https://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc&api_key=xxxxxxxxxxxxxxxxxxxxx&page=" + index ); } });
The first example is not working because API_URL is a const, so it wont change.第一个例子不起作用,因为 API_URL 是一个常量,所以它不会改变。 You should try using a let.
您应该尝试使用 let。
var index = 1;
const main = document.getElementById("main");
let API_URL =
"https://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc&api_key=xxxxxxxxxxxxxxxxxxxx&page=" + index;
next.addEventListener("click", () => {
index = index + 1;
getMovies(API_URL);
});
prev.addEventListener("click", () => {
if (index == 1 || index == 0) {
alert("not working");
} else {
index = index - 1;
getMovies(
"https://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc&api_key=xxxxxxxxxxxxxxxxxxxxx&page=" + index
);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.