Why function does not work when you press the button?

Why when you call the function so it works:
setTimeout(loadcomment, 15000, p kolovo);
And when it is called so, then no:
$("#comment-load").click(function (){ loadcomment(p kolovo);});

Use "onclick="loadcomment(p kolovo)" does not help too.
The function itself:
function loadcomment(userpade, load){
kolovo++;
$.ajax({
 url: '../backend/loadcomm.php',
 type: 'POST',
 dataType: 'html',
 data: {
 name: userpade,
 loadvar: load
},
 success: function(data){
 var com = JSON.parse(data);
 for(var i = 0; i < com.length; i++){
 var comment = renderdiv('comment-block', com[i].sendname, com[i].text, com[i].date);

 var comments = document.getElementById("comments");

comments.appendChild(comment);
}
}
});
}
March 20th 20 at 11:33
2 answers
March 20th 20 at 11:35
$("#comment-load").click(e=>console.log('A click actually works for you?'));


$("#comment-load").click(function (){
 console.log('the Function should work');
 loadcomment(p kolovo);
 console.log('This worked');
});
All messages are shown, but the function never works (with setTimeout still works). - chelsey.Kuphal commented on March 20th 20 at 11:38
@Aniya_Hartmann, if deployed, something similar needs to be done inside loadcomment.
To check on the success that got com, output data for each iteration of the loop, etc. - Maci commented on March 20th 20 at 11:41
March 20th 20 at 11:37
Internal function(ajax request) is asynchronous and requires time to complete.
The answer comes immediately, those it works(sends request), but does not have time to wait for a response and cut off memory
With a timer it time, play around with async/await
But she still manages to work, all data is shown. - chelsey.Kuphal commented on March 20th 20 at 11:40
@Aniya_Hartmann, then there are errors in the console with the unknown "kolovo", arise?
function loadcomment(userpade, load){
load++;
$.ajax({
 url: '../backend/loadcomm.php',
 type: 'POST',
 dataType: 'html',
 data: {
 name: userpade,
 loadvar: load
},
 success: function(data){
 var com = JSON.parse(data);
 for(var i = 0; i < com.length; i++){
 var comment = renderdiv('comment-block', com[i].sendname, com[i].text, com[i].date);

 var comments = document.getElementById("comments");

comments.appendChild(comment);
}
}
});
}

So what does work? - Max_Schumm commented on March 20th 20 at 11:43
@nova, no increase should it "kolovo" because the idea is that each time you press the button, load the next 50 reviews. Tried to add alert('something"); in this function, for some reason when the page loads the alert() is shown twice, although the function is called only 1 time. - chelsey.Kuphal commented on March 20th 20 at 11:46

Find more questions by tags JavaScript