Search code examples
jqueryajaxtyped

Ajax called twice


I using typed.js to type different messages on screen. I use this also to typewrite the time and other parameters from the server. Well it runs 1-2 times but then it is supposed to output "Hello" "World" and typed out "Hello" "Hello" "World". This one i figures out it is because of twice ajax call.

Here is my function:

$(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);             
        }
    });
}});

And here is the server side data:

<?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);
?>

Solution

  • 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() 
                    {                           
                    }
                });
            }
    

    Well uhm... event.preventDefault(); after createTyped(); just solved the problem... If anyone wants it you can use it right away ! xD --->