Ajax打了两次电话

时间:2015-03-31 16:20:33

标签: jquery ajax typed

我使用typed.js在屏幕上输入不同的消息。我也用它来写服务器的时间和其他参数。那么它运行1-2次但是它应该输出“Hello”“World”并输出“Hello”“Hello”“World”。我发现这是因为两次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 个答案:

答案 0 :(得分: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 ---&gt;

相关问题