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>

图片转代码服务由CSDN问答提供 功能建议

  var countDownDate = new Date(“&lt;?php echo file_get_contents('wipetime.txt');  ?&gt;“)。getTime(); 
   
 
 

我正在尝试制作一个倒数计时器。 文件“wipetime.txt”每隔几天就会更改一次,所以我正在尝试这样做,这样我就可以在页面加载和倒计时时抓取该文件。

问题是,如果我在页面的某个地方回显那个php值,它就可以了。 它显示文件内容。 但是,如果我在countDownDate或者一个警报中回应它,我什么也得不到。 没有错误,完全没有。

我根本看不出任何错误。

编辑:如果您想知道整个事情,请注意 \ n

 &lt;! - 白名单计时器 - &gt; 
&lt; script&gt; 
var countDownDate = new Date(“&lt;?php echo file_get_contents('wipetime.txt');?&gt;”)  .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((距离%(1000 * 60 * 60 * 24))/(1000 * 60 * 60)); 
 var minutes = Math.floor  ((距离%(1000 * 60 * 60))/(1000 * 60)); 
 var seconds = Math.floor((距离%(1000 * 60))/ 1000); 
 document.getElementById(“倒计时”  “).innerHTML =”白名单过期“+天+”d“+小时+”h“
 +分钟+”m“+秒+”s“; 
 if(距离&lt; 0){
 clearInterval  (x); 
 document.getElementById(“countdown”)。innerHTML =“EXPIRED”; 
} 
},1000); 
&lt; / script&gt; 
   
 <  / DIV>
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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();
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题