setTimeout setInterval 详解

 
 
var tttt=setTimeout('northsnow()',1000);//执行一次
clearTimeout(tttt);//清除定时
var tttt=setInterval('northsnow()',1000);//循环执行
clearInteval(tttt);//清除定时
定时执行函数 不能有参数 即setTimeout(Dofunction(),1000);
带参数 方式一 : setTimeout("Dofunction(ace)",1000);
方式二 :
setTimeout(_Dofunction(),1000);
function _Dofunction(){
return function (){
Dofunction(ace);
}
}
function Dofunction(ace){
alert(ace);
}

<i></i> <em></em>//斜体
 
<b>注意:</b>加粗

 
文字滚动
方法一。
str+='<div id="buttomdown'+i+'" style="position:absolute;left:'+left+'px;top:'+(top+101)+'px;138px;height:24px;background:url(imgs/down.png) no-repeat;text-align:center;z-index: 99;white-space:nowrap;overflow:hidden;"><font id="fontOne'+i+'" class="downfont">'+f_cc_name+'</font><font id="fontTwo'+i+'" class="downfont"></font></div>';
 
var area="";
var con1="";
var con2="";
var scrollData=[];
var adc="";
function getScrollData(){
scrollData.splice(0,scrollData.length);//清空数组
for(var i=0;i<jsonList.length;i++){//获取所有滚动数据
area =document.getElementById('buttomdown'+i);
con1 = document.getElementById('fontOne'+i);
con2 = document.getElementById('fontTwo'+i);
if(con1.offsetWidth>area.offsetWidth){//超过div宽度 开始滚动
scrollData.push(i);
}
}
clearInterval(adc);
adc=setInterval(scrollLeft,50);
}
function scrollLeft(){
for(var i=0;i<scrollData.length;i++){
var a=scrollData[i];
area =document.getElementById('buttomdown'+a);
con1 = document.getElementById('fontOne'+a);
con2 = document.getElementById('fontTwo'+a);
con2.innerHTML=con1.innerHTML;
if(area.scrollLeft>=con1.offsetWidth){
area.scrollLeft=0;
}else{
++area.scrollLeft;
//console.log(area.scrollLeft+"="+con1.offsetWidth);
}
}
}
 
area.onmouseover=function(){//清除定时器 clearInterval(mytimer); } area.onmouseout=function(){//启动定时器 mytimer=setInterval(scrollUp,time); }
方法二。
<marquee></maequee> 标签可以实现滚动
 
js 获取字符串长度 (http://www.jb51.net/article/48202.htm
把双字节的替换成两个单字节的然后再获得长度 
复制代码代码如下:
getBLen = function(str) {
  if (str == null) return 0;
  if (typeof str != "string"){
    str += "";
  }
  return str.replace(/[^x00-xff]/g,"01").length;
}
 
var len = getBLen(f_cc_name);//大于div宽度的滚动
if(len*14>206){//字节数*字体大小>div宽度*2
str+='<div id="buttomdown'+i+'" style="position:absolute;left:'+left+'px;top:'+(top+118)+'px;103px;height:20px;background:url(imgs/down.png) no-repeat;text-align:center;z-index: 99;white-space:nowrap;overflow:hidden;"><marquee direction=left behavior=scroll loop=-1 scrollamount=2 ><font id="fontOne'+i+'" class="downfont">'+f_cc_name+'</font></marquee></div>';
}else{
str+='<div id="buttomdown'+i+'" style="position:absolute;left:'+left+'px;top:'+(top+118)+'px;103px;height:20px;background:url(imgs/down.png) no-repeat;text-align:center;z-index: 99;white-space:nowrap;overflow:hidden;"><font id="fontOne'+i+'" class="downfont">'+f_cc_name+'</font></div>';
}
 
原文地址:https://www.cnblogs.com/leo0705/p/8473088.html