jQuery(function($) { $("#addForm").submit(function(e) { e.preventDefault(); registr(); }); }) function registr() { var data = $("#addForm").serialize() $.ajax({ url: '/script.php', data: data, success: function(data) { $('.progress').show(); $('button[type="submit"]').prop('disabled', true); $('#addForm').find('input[type=text]').val(''); $('.alert-info').show(); timer(data); } }); function timer(site) { var elem = document.getElementById('progress'); elem.value = parseInt(elem.value) + 1; if (elem.value < 1) { window.setTimeout(timer, 1000); } else { $('.progress').hide(); $('button[type="submit"]').prop('disabled', false); $('.alert-info').hide(); var url = "/project/" + site + "/"; $(location).attr('href', url); } } } 

If set if (elem.value <10) then undefined is passed to the site variable and transferred to / project / undefined /

    1 answer 1

    You pass a timer function reference to setTimeout . The javascript engine, naturally, does not know with which parameters it is called and is called without parameters.

     window.setTimeout(function(){ timer(site); }, 1000); 

    or

     window.setTimeout(timer.bind(window, site), 1000); 

    or, as @Grundy rightly pointed out,

     window.setTimeout(timer, 1000, site); 

    Maybe you meant to make progress visible before calling $.ajax ?

    • better: window.setTimeout(timer, 1000, site) - an extra function is not created - Grundy
    • @Grundy Yes, it constantly flies out of my head, that you can immediately specify the parameters in setTimeout - Igor