js runs too fast how do i put it to execute through time

<button id="button" onclick="$(function() { var chat_scroll = $('#content'); chat_scroll.scrollTop(chat_scroll.prop('scrollHeight')); });" >send</button> 

not all code

 <div class="box"> <div id="content"><?php include("chatGetMsg.php"); ?></div> <div id="textarea"><textarea id="text" name='message' maxlength="200"></textarea></div> <div id="send"><button id="button" onclick="$(function() { var chat_scroll = $('#content'); chat_scroll.scrollTop(chat_scroll.prop('scrollHeight')); });" >send</button></div> </div> <div class="Chan"></div> 

ajax

  $(document).ready(function () { show(); setInterval(show, 300); $("#button").bind("click", function () { var message = $("#text").val(); $.ajax({ url: "chatGetMsg.php", type: "POST", data: {message}, dataType: "json" }); }); function show() { $.ajax({ url: "chatGetMsg.php", cache: false, success: function (html) { $("#content").html(html); } }); } }); 
  • Probably because the 'content' you have not initialized by your element. Errors in the console? - koks_rs 3:09
  • I changed the subject of the question - NoProgress
  • Maybe you need setTimeout? What does "running too fast" mean? - KRasul
  • my ajax script does not have time to execute, as it already scrolls down - NoProgress
  • And you call the scroll function at the end of the ajax request. - KRasul

1 answer 1

In my opinion, the decision to execute the code more slowly is not entirely correct. Since they usually want the opposite, so that the code would work faster;) It would be more correct to add a timer to the event. For example, this is used for keyup, keydown, keypress. One event triggers a timer, if its value has reached the desired value, then an event is performed, otherwise, if the keyboard continues to be pressed, the timer is reset and starts counting from zero, and so on.