Bartosz Wyględacz
Bartosz Wyględacz

Reputation: 589

document.ready page reload

I'm starting my adventure with js and jQuery and I can't understand why my function pokaz_czas() is not executed when I reload the page.

I use a script in a php file (wpis.php), which I'm including in another php file (index.php?page=wpis). I want my function to execute when I reload the page (index.php?...)

Javascript Code:

  function zero(element) 
{ //1
if (element < 10) return element = "0" + element;
return element;
}
function pokaz_czas() 
{
    var czas = new Date();
    var data = czas.getFullYear()+"-"+zero((czas.getMonth()+1))+"-"+zero(czas.getDate());
    var time = zero(czas.getHours())+":"+zero(czas.getMinutes());

    $('#data').val(data);
    $('#time').val(time);

}
$(document).ready(pokaz_czas());

$(document).ready
(
function()
{       
    $('#data').change
        (
            function()
            {
                alert($(this).val());
            }
        );
}
);

By the way, if I do...

 ...
 var time = zero(czas.getHours())+":"+zero(czas.getMinutes());

    $('#data').val(data);
    $('#time').val(time);
    setTimeout("pokaz_czas()",1000)
}

...then the function is executing (but it does so every second, and I want to execute it only once).

Sorry for my English, it' s my first post here ;) Thank you in advance.

Upvotes: 1

Views: 1007

Answers (1)

Ivan Chernykh
Ivan Chernykh

Reputation: 42176

Yes , definitely try this way:

$(document).ready(pokaz_czas);

It means you don't want to fire pokaz_czas immediately , only when your DOM is ready

Upvotes: 4

Related Questions