华为路由器设置-晨枫u盘维护

settimeinterval
2023年4月4日发(作者:wifi无线网络)

setInterval⽤法注意

functionpublic_showmsg(){

vartimestamp,onlineTime;

timestamp=(newDate())/1000;//获取当前时间的时间戳

onlineTime=timestamp-last_login_time;

$(".chat-conv").html('');

$(".tixingBox").html('');

varchangeUrl="?n=index&h=showmsg"+"&sid="+();

$.get(changeUrl,function(str){

c_msg=str;

switch(str){

case"no_login":

//alert('未登陆,转向登陆页');

//="?n=login";

break;

case'0':

$(".chat-conv").html("

您有0条新消息

");

$(".tixingBox").html("最新提醒(0)");

break;

default:

strArr=('|');

$(".chat-conv").html(strArr[0]);

$(".tixingBox").html(strArr[1]);

//播放提⽰声⾳

if(strArr[2]=='new_message'){

try{play("/public/system/flash/new_3");}catch(error){}

//play("/public/system/flash/new_3");

}

if($.cookie('cmsg')!=c_msg&&$.cookie('cmsg')!=''){

//展开

$('.chat-window').slideDown();

$.cookie('thestaus','0');

$('.tixingBox').addClass("tixingBox3");

}

elseif($.cookie('thestaus')!=1){

$('.chat-window').slideDown();

$('.tixingBox').addClass("tixingBox3");

}

break;

}

});

/*1、模拟频率:

1.1在线五分钟以内1分钟模拟⼀次;

1.2在线10分钟以内2分钟模拟⼀次;

1.3在线10-30分钟3分钟模拟⼀次;

1.430分钟以上4分钟⼀次;

1.5每次模拟都必须显⽰,不沿⽤之前的概率原则

*/

//setInterval("public_showmsg();",6000);

if(onlineTime<300){

if(k)clearTimeout(k);

k=setTimeout("public_showmsg();",60000);

}elseif(onlineTime>=300&&onlineTime<600){

if(k)clearTimeout(k);

setTimeout("public_showmsg();",120000);

}elseif(onlineTime>=600&&onlineTime<1800){

if(k)clearTimeout(k);

setTimeout("public_showmsg();",180000);

}else{

if(k)clearTimeout(k);

setTimeout("public_showmsg();",60000);

}

}

$(function(){

public_showmsg;

});

如果setTimeout⽤setInterval替代,直接会导致机器死机,cpu占有过⼤,原因是:

setInterval是,每调⽤⼀次,多⼀个循环

setInterval是定时执⾏的,

第1次运⾏public_showmsg后,启动了setInterval,6秒后再次调⽤public_showmsg,⼜启动⼀次setInterval并调⽤public_showmsg,如此

循环下去,CPU当然吃不消了

如果是setInterval就不应该放在函数内部,如果要放在函数内部就应该⽤setTimeout

更多推荐

settimeinterval