首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用ajax在站点中显示通知和打开弹出窗口,使站点变慢或锁定mysql

使用ajax在站点中显示通知和打开弹出窗口,使站点变慢或锁定mysql
EN

Stack Overflow用户
提问于 2017-11-11 14:48:27
回答 1查看 701关注 0票数 1

我正在使用drupal作为网站。我已经编写了自定义模块,在浏览器中显示通知,并在网站上打开弹出窗口。

我使用的是JavaScript函数,如果条件为假或出现错误,则每15秒或更短时间调用一次。

问题是,如果有5个页面是打开的,那么在所有页面上设置超时功能是每15秒或更短的时间在所有页面上调用一次,并使网站变慢或中断。

请给出一些说明,以提高本网站的性能。

代码语言:javascript
运行
复制
(function ($) {
  $(function() {
    // ä»»ä½•éœ€è¦æ‰§è¡Œçš„js特效
    setTimeout("countSecond2()", 15000);

    $('body').on('click','#popClose',function(event){
      //jQuery(".pop_content_message").click(function() {
      $(this).parents('#pop').remove();
    });

  });
})(jQuery);

function countSecond() {
  var data = '';
  var need_delay = 0;
  var js_common_messages_popup = '/messages/popup';
  jQuery.ajax({
    url: js_common_messages_popup,
    type : "post",
    dataType : "text",
    data : data,
    async: true,
    success: function(msg) {
      var Obdata = jQuery.parseJSON(msg);
      if(Obdata !=null){
    //alert('1p='+JSON.stringify(msg));
    var call = Obdata.call;
    var output = Obdata.output;
    var selectbox = Obdata.selectbox;
    var checkbox = Obdata.checkbox;
    var url = Obdata.url;
    var need_delay = Obdata.need_delay;
    if (call == '1') {
      browser_notification(Drupal.t('You have a new reminder'), '', url);

      jQuery('#id_pop_task').prepend(output);
      jQuery('#id_pop_task .pop_content_message').show();
      /*jQuery('#id_pop_task').html(output);
      jQuery('#pop').show();
      jQuery("#popClose").click(function() {
        jQuery('#pop').hide();
      });*/

      if (selectbox == '1') {
        //jQuery("#antiStress").selectbox();
            jQuery("[id=antiStress]").selectbox();
      }
      if (checkbox == '1') {
        jQuery('.customR input[type="checkbox"]').ezMark({checkboxCls: "ez-checkbox-green", checkedCls: "ez-checked-green"});
      }
      setTimeout("countSecond2()", 15000);
    }else {
      countSecond2();
    }
      }
      else {
    countSecond2();
      }
    },
    beforeSend: function() {
      //jQuery('#loading').hide();
    },
    complete: function() {
    },
    error: function() {
      countSecond2();
      //location.reload();
    }
  });
}

function countSecond2() {
  var data = '';
  var need_delay = 0;
  var js_common_messages_popup = '/messages/popup2';
  jQuery.ajax({
    url: js_common_messages_popup,
    type : "post",
    dataType : "text",
    data : data,
    async: true,
    success: function(msg) {
      var Obdata = jQuery.parseJSON(msg);
      if(Obdata !=null){
    //alert('2p='+JSON.stringify(msg));
    var call = Obdata.call;
    var output = Obdata.output;
    var selectbox = Obdata.selectbox;
    var checkbox = Obdata.checkbox;
    var url = Obdata.url;
    var need_delay = Obdata.need_delay;
    if (call == '1') {
      browser_notification(Drupal.t('You have a new reminder'), '', url);

      jQuery('#id_pop_task').prepend(output);
      jQuery('#id_pop_task .pop_content_message').show();

      /*jQuery('#id_pop_task').html(output);
      jQuery('#pop').show();
      jQuery("#popClose").click(function() {
        jQuery('#pop').hide();
      });*/
      if (selectbox == '1') {
        //jQuery("#antiStress").selectbox();
            jQuery("[id=antiStress]").selectbox();
      }
      if (checkbox == '1') {
        jQuery('.customR input[type="checkbox"]').ezMark({checkboxCls: "ez-checkbox-green", checkedCls: "ez-checked-green"});
      }
      setTimeout("countSecond2()", 15000);
    }else {
      countSecond();
    }
      }
      else {
    countSecond();
      }
    },
    beforeSend: function() {
      //jQuery('#loading').hide();
    },
    complete: function() {
    },
    error: function() {
      countSecond();
      //location.reload();
    }
  });
}
EN

回答 1

Stack Overflow用户

发布于 2017-11-11 15:16:56

在这种情况下,我建议您首先在选项卡中检测到用户处于活动状态,然后在指定时间段内发送ajax请求:

就像这样:

代码语言:javascript
运行
复制
$(window).on("blur focus", function(e) {
var prevType = $(this).data("prevType");

if (prevType != e.type) {  
    switch (e.type) {
        case "blur":
            // do work
            break;
        case "focus":
            // do work
            break;
    }
}

$(this).data("prevType", e.type);
})

如果你想要服务器向客户端推送数据,你也可以使用websocket。

此处提供了有关检测活动选项卡的更多信息:How to tell if browser/tab is active

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47235219

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档