【问题标题】:Ajax called twice阿贾克斯打了两次电话
【发布时间】:2015-06-05 01:54:23
【问题描述】:

我使用 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);
?>

【问题讨论】:

标签: jquery ajax typed


【解决方案1】:
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 --->

【讨论】:

    猜你喜欢
    • 2014-06-18
    • 1970-01-01
    • 2010-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-15
    • 2018-07-04
    • 1970-01-01
    相关资源
    最近更新 更多