简体   繁体   English

在 jQuery 方面需要帮助

[英]Need help with jQuery

This my script:这是我的脚本:

<script type="text/javascript">
var auto_refresh = setInterval(
function () {
$('#alert_div').load('/check_messages.php?timestamp=<?php echo TIMESTAMP2;?>').fadeIn("slow");}, 180000);
$("html,body").scrollTop("alert_div",0);
</script>

Actually, everyting works fine but... As you can see, every 3 minutes script loads check_messages.php file.实际上,everyting 工作正常,但是...如您所见,脚本每 3 分钟加载一次 check_messages.php 文件。 If user has new messages messages check_messages.php outputs information, otherwise the output is empty:如果用户有新消息消息 check_messages.php输出信息,否则 output 为空:

echo "";

I need that $("html,body").scrollTop("alert_div",0);我需要那个$("html,body").scrollTop("alert_div",0); exetutes only in case if php script output is not empty.仅在php 脚本 output不为空的情况下执行。

try that:试试看:

<script type="text/javascript">
var auto_refresh = setInterval(
function () {
$('#alert_div').load('/check_messages.php?timestamp=<?php echo TIMESTAMP2;?>', function(response){
    if(resonse != '')
        $("html,body").scrollTop("alert_div",0);
}).fadeIn("slow");}, 180000);
</script>

The load function takes a function as a parameter, which is called when the request completes with the response text as the first parameter.加载function 以 function 作为参数,在请求完成时以响应文本作为第一个参数调用该参数。 You can check whether the response was empty in this callback function and scrolling to the top if not then.您可以在此回调 function 中检查响应是否为空,如果不是则滚动到顶部。

Requests such as load are handled asynchronously, which means that the code that is immediately after it will be run before the call has completed.诸如load之类的请求是异步处理的,这意味着紧随其后的代码将在调用完成之前运行。 If you want something to happen after the call has completed then you need to include it in the completion callback.如果您希望在调用完成后发生某些事情,那么您需要将其包含在完成回调中。 For this reason I do not expect your fadeIn call to work, as I don't think the load function returns the included element.出于这个原因,我不希望您的 fadeIn 调用起作用,因为我认为加载 function 不会返回包含的元素。 When the load call returns, your text has not been included in the document.load调用返回时,您的文本尚未包含在文档中。

I've also taken the liberty of moving the code out of your anonymous function into one called check_messages .我还冒昧地将代码从您的匿名 function 移到名为check_messages的代码中。 In your code you had the scroll to top outside the setInterval call so it was being executed immediately, rather when the messages were checked.在您的代码中,您可以在setInterval调用之外滚动到顶部,因此它会立即执行,而不是在检查消息时执行。

<script type="text/javascript">
function check_messages() {
    $('#alert_div').load('/check_messages.php?timestamp=<?php echo TIMESTAMP2;?>',
         function(responseText, textStatus, XMLHttpRequest) {
              if(responseText != "") {
                  $("html,body").scrollTop("alert_div",0);
              }
         }).fadeIn("slow");
}

var auto_refresh = setInterval(check_messages, 180000);
</script>

You'll need to use something like:你需要使用类似的东西:

$.get('/check_messages.php?timestamp=<?php echo TIMESTAMP2;?>', function(data) {

  if(data) {
     $('#alert_div').replace(data); 
     $("html,body").scrollTop("alert_div",0);
  }
});

instead.反而。

I believe you should use the other version of.load as per here - http://api.jquery.com/load/ .我相信您应该按照此处使用其他版本的 .load - http://api.jquery.com/load/ In the callback, you should check weather there is anything in the response and perform actions based on that condition.在回调中,您应该检查天气响应中是否有任何内容并根据该条件执行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM