简体   繁体   English

用php倒计时javascript

[英]countdown javascript with php

I want a countdown on my website that starts with a value from php (out of a MySQL database). 我想在我的网站上倒数,以php(MySQL数据库中)的值开头。

Currently I use the following function: 目前,我使用以下功能:

<script type="text/javascript">
  var seconds;
  var temp;
  var tijd = <? echo json_encode($t); ?>
 
  function countdown() {
    seconds = document.getElementById('countdown').innerHTML;
    seconds = parseInt(seconds, tijd);
 
    if (seconds == 1) {
      temp = document.getElementById('countdown');
      temp.innerHTML = "0";
      return;
    }
 
    seconds--;
    temp = document.getElementById('countdown');
    temp.innerHTML = seconds;
    timeoutMyOswego = setTimeout(countdown, 1000);
  } 
 
  countdown();
</script>

But in case $t is not 10 the value is increasing with strange steps. 但是如果$ t不是10,则该值会以奇怪的步长递增。 How can I solve this? 我该如何解决? I don't have experience with javascript, so that's the problem. 我没有使用javascript的经验,所以这就是问题所在。

Besides that, when I adding this script, the following code: 除此之外,当我添加此脚本时,以下代码:

header('Content-Type: text/html; charset=iso-8859-1');

isn't working anymore, because for example " ë " is represented with a ? 不再工作,因为例如用“?”表示? in the text. 在文本中。 I don't know why this is happening? 我不知道为什么会这样?

Who can helps me? 谁可以帮助我? Thanks in advance. 提前致谢。

If you want a human-readable counter you must use 10 in the second parameters of parseInt function 如果要让人类可读的计数器,则必须在parseInt函数的第二个参数中使用10

"Specify 10 for the decimal numeral system commonly used by humans." “为人类常用的十进制数字指定10。”

Then the only number you want to retrieve from database is the start of the counter. 然后,您要从数据库中检索的唯一数字是计数器的开头。

I think you have some like this in your HTML code: 我认为您的HTML代码中有类似的内容:

<div id="countdown"><?php echo $startCounterFromDB; //This is the start of the counter ?></div>

And in you want to keep your code, in your JavaScript you only have to change the second parameter of the parseInt function. 而且,如果要保留代码,则只需在JavaScript中更改parseInt函数的第二个参数即可。

<script type="text/javascript">
  var seconds;
  var temp;
  var tijd = 10; //always use 10

  function countdown() {
    seconds = document.getElementById('countdown').innerHTML;
    seconds = parseInt(seconds, tijd);

    if (seconds == 1) {
      temp = document.getElementById('countdown');
      temp.innerHTML = "0";
      return;
    }

    seconds--;
    temp = document.getElementById('countdown');
    temp.innerHTML = seconds;
    timeoutMyOswego = setTimeout(countdown, 1000);
  } 

  countdown();
</script>

Finally UTF-8 is a PSR standard for all files, but you don't need to use json_encode function, because you only need to print a single number. 最后,UTF-8是所有文件的PSR标准,但是您不需要使用json_encode函数,因为您只需要打印一个数字即可。

This code will work. 此代码将起作用。

关于PREMADE美丽的jquery倒计时呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM