drn5375 2018-09-30 22:36
浏览 64
已采纳

无法在javascript中回显php值

var countDownDate = new Date("<?php echo file_get_contents('wipetime.txt');?>").getTime();

I'm trying to make a countdown timer. The file "wipetime.txt" changes every few days so I'm trying to make it so I can grab the file when the page loads and countdown to that date.

The problem is that if I echo that php value somewhere on the page, it works. It displays the file contents. However, if I were to echo it inside countDownDate or even an alert, I get nothing. No error, just completely nothing.

I can't see anything wrong at all.

Edit: Heres the entire thing in case you wanna know

<!--Whitelist timer-->
<script>
var countDownDate = new Date("<?php echo file_get_contents('wipetime.txt');?>").getTime();
var x = setInterval(function() {
  var now = new Date().getTime();
  var distance = countDownDate - now;
  var days = Math.floor(distance / (1000 * 60 * 60 * 24));
  var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = Math.floor((distance % (1000 * 60)) / 1000);
  document.getElementById("countdown").innerHTML = "Whitelist expires in " + days + "d " + hours + "h "
  + minutes + "m " + seconds + "s ";
  if (distance < 0) {
    clearInterval(x);
    document.getElementById("countdown").innerHTML = "EXPIRED";
  }
}, 1000);
</script>
  • 写回答

1条回答 默认 最新

  • dpr77335 2018-09-30 22:56
    关注

    There is a possibility that the file wipetime.txt contains some special characters (maybe a carriage return). Two things you can do:

    1. Manually check the file for any artifacts and remove them.
    2. You should trim the content.

    The code:

    <?php
    $content = file_get_contents('wipetime.txt');
    $timeStr = trim($content); // should remove trailing characters like new lines. 
    ?>
    
    var countDownDate = new Date("<?php echo $timeStr;?>").getTime();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 win32如何自绘编辑框的背景图片(语言-c++|操作系统-windows)
  • ¥15 c# 直接使用 c++ 写的 class
  • ¥15 微信夜间被转走了1w对,当天手机剪切板里就出现了这个乱码,有铁子可以看看是啥吗可以
  • ¥50 跑通github上的代码 深度学习 pytorch
  • ¥50 求写,批处理调用分区助手分区脚本
  • ¥15 求购HI3519AV100开发板
  • ¥15 请问1553 RT怎么测试,没有BC有方法吗
  • ¥100 业务编程如何选择学习方向和内容?
  • ¥15 wamp3.3.5安装完成后图标正常显示绿色,鼠标左右键点击图标均无反应。求解决方法。
  • ¥15 鼠标点击的这条记录了什么?