I make online consultant for chat. Messages from the user every 2 seconds are loaded via AJAX (I don’t need to shout that they don’t do this anymore, I know). So, I use the script:

function show() { $.ajax({ url: "<?bloginfo('template_url');?>/load_messes.php", cache: false, success: function(html){ $("#result_div_id").html(html); $i++; } }); } $(document).ready(function(){ show(); setInterval('show()',2000); }); 

And here is the message update code (load_messes.php):

 <?php @ini_set('display_errors','Off'); @ini_set('error_reporting',0); define('WP_USE_THEMES', false); require( $_SERVER['DOCUMENT_ROOT'] .'/wp-blog-header.php'); global $wpdb; ?> <?php $_SESSION['session'] = $_SERVER["REMOTE_ADDR"]; $_SESSION['session'] .= session_id(); $session = $_SESSION['session']; $messages = $wpdb->get_results(' SELECT * FROM euro_messages WHERE SESSION = "'.$session.'" '); $seen = $wpdb->get_var(' SELECT id FROM euro_messages WHERE SESSION = "'.$session.'" and seen = 0 '); $seen_2 = $wpdb->get_var(' SELECT id FROM euro_messages WHERE SESSION = "'.$session.'" and seen = 0 and fromto > 0 '); $templatepath = get_bloginfo('template_url'); if($seen){ echo '<script> $(function () { setTimeout(function () { var $container = $(\'.container\'); $container[0].scrollTop = $container[0].scrollHeight; }, 100); }); </script>'; } if($seen_2){ echo ' <script> setTimeout(function soundClick() { var audio = new Audio(); // Создаём новый элемент Audio audio.src = \''.$templatepath.'/sounds/sms.mp3\'; // Указываем путь к звуку "клика" audio.autoplay = true; // Автоматически запускаем }, 100); </script> '; } for ( $d = 0; $d < count($messages); $d++) { $mess = $messages[$d]->message; $fromto = $messages[$d]->fromto; if($fromto == 0){ $mess_text = "<h4 style='margin:0;padding:0;'>Вы:</h4><font size=2>$mess</font><hr>"; }else{ $mess_text = "<h4 style='margin:0;padding:0;'>Консультант:</h4><font size=2>$mess</font><hr>"; } echo $mess_text; $wpdb->update( 'euro_messages', array( 'seen' => 1 ), array( 'session' => $session ) ); } if(!$messages){ echo '<font size=2 style="text-align: left;"> <i> Для начала диалога отправьте сообщение. </i> </font> '; } ?> 

My site is on the VP. When I send the message, it goes in essence, the same script, only there is loading another file - form.php. The message of the user is transferred to it and it is added to the database. But the output of load_messes.php for some reason works only in Chrome. Thank you in advance)

  • load_messes.php or load-messes.php? The code in your question is simple, assuming that everything is fine around it, for example, $i somewhere declared and initialized, everything should work. Add consol.log($i + ": " + html); in success . - Igor
  • $ i is initialized there much earlier and is not needed at all for this script, it has nothing to do with it) - it is needed in order to automatically drop the messages down - see for yourself: var $ i = 0; function show () {$ .ajax ({url: "<? bloginfo ('template_url');?> / load_messes.php", cache: false, success: function (html) {$ ("# result_div_id"). html (html); $ i ++;}}); } --- continued in the comment below --- - Dan eStet
  • $ (document) .ready (function () {show (); setInterval ('show ()', 2000);}); </ script> <script> $ (function () {setInterval (function () {if ($ i == 1) {var $ container = $ ('. container'); $ container [0] .scrollTop = $ container [0] .scrollHeight; $ i ++;}}, 100);}); </ script> - Dan eStet
  • may not work due to the fact that the site is on LAN, as it did on its other site (on the host) a similar structure in the file - everything worked out in chrome and in the opera)) - Dan eStet
  • I feel the show function in <script></script> . Most likely not the correct url. url: "<?php echo bloginfo('template_url'); ?>/load_messes.php", or url: "<?=bloginfo('template_url');?>/load_messes.php", - Mr. Black

1 answer 1

Use complete:

 $(document).ready(function(){ show(); }); function show() { $.ajax({ url: "<?= bloginfo('template_url'); ?>/load_messes.php", cache: false, success: function(data) { $("#result_div_id").html(html); }, complete: function() { setTimeout(show, 2000); } }); } 
  • Does this explain why it works only in Chrome? - Igor
  • It stopped working even in Chrome) - Dan eStet
  • There is nothing to say about other browsers - everything costs a stake - Dan eStet
  • 3
    Great, now it means that all browsers behave in the same way, which was required :) - Igor
  • @Igor, yes, now it's much better xD)) - Dan eStet