在第三方脚本的wordpress中获取userinfo的CPU过载

时间:2015-01-12 17:06:20

标签: javascript php wordpress cpu-usage

我试图通过以下代码在shoutbox脚本中从wordpress获取userinfo:

include ('../../wp-load.php'); 
global $current_user;
$current_user = wp_get_current_user();
$user_ID = $current_user->ID;
$user_identity = $current_user->user_login;

然后在脚本中的每个函数上我都会调用

global $user_ID;

问题是cpu过载,因为 shoutbox脚本每5秒调用一次

有没有解决方案可以解决这个问题!

enter image description here

Shoutbox由我自己制作:

这是javascript核心:

<script type="text/javascript">
   var count = 0;
   var directory = '';
   var lastTime = 0;

    function prepare(response) {
        var d = new Date();
          count++;
          d.setTime(response.time*1000);
          var mytime = d.getHours()+':'+d.getMinutes()+':'+d.getSeconds();

        if(decodeHTMLEntities(response.color) === "deleted"){

          $("#time-"+decodeHTMLEntities(response.message)).css('background','#FFE3E6');
          $("#time-"+decodeHTMLEntities(response.message)).slideUp("slow");
          $("#time-"+decodeHTMLEntities(response.message)).parent().fadeOut("slow");

        }else if(decodeHTMLEntities(response.color) === "banned"){

          var jsonmsg = response.message.split(',');
           for(z=0; z < jsonmsg.length; z++) {
            if(jsonmsg[z].length){
              $("#time-"+jsonmsg[z]).css('background','#FFE3E6');
              $("#time-"+jsonmsg[z]).slideUp("slow");
              $("#time-"+jsonmsg[z]).parent().fadeOut("slow");
                }
            }

          }else{

          var string = '<li class="Shout-Swatch-'+decodeHTMLEntities(response.color)+' '+decodeHTMLEntities(response.adminClass)+' " id="list-'+count+'"><div id="time-'+decodeHTMLEntities(response.time)+'"><div class="shoutbox-list-nick"><a id="'+decodeHTMLEntities(response.nickname)+'" href="javascript:void(0)"  ><img  class="userlidiv" src="'+decodeHTMLEntities(response.avatar)+'" alt="'+decodeHTMLEntities(response.nickname)+'" title="'+decodeHTMLEntities(response.nickname)+'" id="'+decodeHTMLEntities(response.nickname)+'" ><br class="clear">'+decodeHTMLEntities(response.nickname)+'</a><br class="clear">';


              string  += '</div><div class="shoutbox-list-message">'+response.message+'</div><div class="clear"></div></div></li>';

          return string;
          }
      }

      function decodeHTMLEntities (str) {
                return str;
      }

    function shoutcontrol(st){
    if(st=1){
    clearInterval(timeoutID);
    $("#shoutdiv").css({ opacity: 0.5 });
     $(".shoutcontrol").children().attr("src", "images/play.png");
    $(".shoutcontrol").attr("href","javaScript:shoutcontrol2(1)");
    }else{
        return false;
    }
    }
    function shoutcontrol2(st){
    if(st=1){
    $("#shoutdiv").css({ opacity: 1});
    $(".shoutcontrol").children().attr("src", "images/pause.png");
    timeoutID = setTimeout(updateShoutbox, 3000);
    $(".shoutcontrol").attr("href","javaScript:shoutcontrol(1)");
    }else{
        return false;
    }
    }


    function updateShoutbox() {
      $.getJSON(directory+"index.php?action=update&time="+lastTime, function(json) {

        if (json && json.length) {

        if(json === "closed"){
            $('#shoutdiv').html("<div class='error'>ShoutBox is out of work. Please check this section later.<br>We will be back ASAP.</div>");
            return false;
         }else{

          for(i=0; i < json.length; i++) {
            $('#shoutdiv').append(prepare(json[i]));
            $('#list-' + count).fadeIn('slow');
            var objDiv = document.getElementById("shoutdiv");
            objDiv.scrollTop = 100000;
            }
             var j = i-1;
            lastTime = json[j].time;


        }

        }

      });
      timeoutID = setTimeout(updateShoutbox, 3000);

    }
function shoutError(err) {
  $('#systemalert').css("display","block");
  $('#systemalert').html('<strong>err!</strong>'+err).stop().slideDown('fast').delay(15000).slideUp('slow');
  }  

$(document).ready(function() {
    timeoutID = setTimeout(updateShoutbox, 100);
});
</script>

0 个答案:

没有答案