使用 JavaScript 递增页码
Incrementing page number with JavaScript
除了从 1 到 2 之外,我试图弄清楚为什么以下代码不增加页码,但我找不到答案。
<script>
var n = 1;
function increment() {
return ++n;
}
function countDown(secs,elem) {
var element = document.getElementById(elem);
element.innerHTML = "Timp rămas: "+secs+" secunde.";
if(secs < 1) {
clearTimeout(timer);
window.location.replace('question.php?n='+increment(n));
element.innerHTML += '<a href="#">Click here now</a>';
}
secs--;
var timer = setTimeout('countDown('+secs+',"'+elem+'")',1000);
}
</script>
想法?
试试这个:
var params = new URL(document.location).searchParams;
var n = params ? params.get('n') || 1 : 1;
所以 n 是从 URL 读取的,而不是每次加载页面时都设置为 1。
编辑:啊,也许:
var timer = setTimeout(function(){countDown(secs, elem);},1000);
除了从 1 到 2 之外,我试图弄清楚为什么以下代码不增加页码,但我找不到答案。
<script>
var n = 1;
function increment() {
return ++n;
}
function countDown(secs,elem) {
var element = document.getElementById(elem);
element.innerHTML = "Timp rămas: "+secs+" secunde.";
if(secs < 1) {
clearTimeout(timer);
window.location.replace('question.php?n='+increment(n));
element.innerHTML += '<a href="#">Click here now</a>';
}
secs--;
var timer = setTimeout('countDown('+secs+',"'+elem+'")',1000);
}
</script>
想法?
试试这个:
var params = new URL(document.location).searchParams;
var n = params ? params.get('n') || 1 : 1;
所以 n 是从 URL 读取的,而不是每次加载页面时都设置为 1。
编辑:啊,也许:
var timer = setTimeout(function(){countDown(secs, elem);},1000);