1

I have a script and jQuery Ajax call like:

<script type="text/javascript">
 $(document).ready(function() {
  var timer;
  $.ajax({
     type: 'POST',
         url: 'closettime.php',
         success: function( res ){
         var json = JSON.parse(res);
             timer = json["datecounter"];
             alert(timer);
         }
    });   
 var date = new Date(timer);
 var now   = new Date();                
 var diff  = date.getTime()/1000 - now.getTime()/1000;
 var clock = $('.clock').FlipClock(diff, {
    clockFace: 'HourlyCounter',
    countdown: true,
    showSeconds: true
 });

});
</script>

now my problem is I am not able to assign the time value in var date = new Date(timer); in ajax success callback as

timer = json["datecounter"];

can you please let me know how to fix this? thanks

4

1 回答 1

0

问题很可能是在分配日期变量的代码完成之后请求才完成,因此存在未分配计时器的错觉。将所有依赖于计时器变量的代码移到回调中,这应该可以工作,或者至少可以解决其中一个问题。

<script type="text/javascript">
$(document).ready(function() {
    $.ajax({
       type: 'POST',
       url: 'closettime.php',
       success: function( res ){
           var json = JSON.parse(res);
           var timer = json["datecounter"];
           alert(timer);
           var date = new Date(timer);
           var now   = new Date();                
           var diff  = date.getTime()/1000 - now.getTime()/1000;
           var clock = $('.clock').FlipClock(diff, {
              clockFace: 'HourlyCounter',
              countdown: true,
              showSeconds: true
           });
       }
    });
});
</script>
于 2014-06-10T00:07:38.110 回答