2015-03-31 107 views
0

我使用typed.js在屏幕上键入不同的消息。我也使用它来打印服务器的时间和其他参数。它运行1-2次,但它应该输出“Hello”“World”并输入“Hello”“Hello”“World”。这一个我认为这是因为两次ajax呼叫。Ajax调用两次

这里是我的功能:

$(document).ready(function() { 
    var data; 
     getData(); 
     createTyped(); 

    function createTyped() { 
     $("#typed").typed({ 
      strings: data, 
      typeSpeed: 30, 
      backDelay: 500, 
      loop: true, 
      contentType: 'html', 
      loopCount: false, 
      callback: function() { 
       //getData(); 
       $("#typed").typed('reset'); 
       getData(); 
       createTyped();          
      },    
      resetCallback: function() { 
      } 
     }); 
    } 

function getData() { 
    jQuery.ajax({ 
     url: "data.php", 
     cache: false, 
     async: false, 
     success: function(result) { 
      data = $.parseJSON(result);    
     } 
    }); 
}}); 

这里是服务器端数据:

<?php 
$today = date("F j, Y, g:i:s a"); 
$array = array('Test', 
       'This is a Test', 
       'Date and time is ' . $today, 
       '' 
       ); 
echo json_encode($array); 
?> 
+1

其中是服务器端代码中的Hello World部分? – 2015-03-31 16:25:11

+1

请阅读[“如何创建最小,完整和可验证示例”](http://stackoverflow.com/help/mcve) – 2015-03-31 17:42:23

回答

0
function createTyped() 
    { 
      $("#typed").typed({ 
      strings: data, 
      typeSpeed: 30, 
      backDelay: 500, 
      loop: true, 
      contentType: 'html', 
      loopCount: false, 
      callback: function() 
      { 
       $("#typed").typed('reset'); 

       getData(); 
       createTyped();   
event.preventDefault();      
       },    
       resetCallback: function() 
       {       
       } 
      }); 
     } 

好呃... ... event.preventDefault();createTyped();刚刚解决了问题...如果有人想要它,你可以马上使用它! xD --->