本文實例講述了JS基于MSClass和setInterval實現ajax定時采集信息并滾動顯示的方法。分享給大家供大家參考,具體如下:
setTimeout 用于延時器,只執行一次.
setInterval:用于多次執行.
項目中引用到jquery.timers-1.2.js定時器,并且MSClass做信息定期滾動,每3分鐘執行一次,執行三次左右,滾動的次數越來越快,原因在于timers和MSClass都用了setInterval,
都會再次添加一個setInterval,最后導致幾個setInterval并發執行了,所以速度很快,需要在調用的時候clearInterval上次的setInterval ,或者引用MSClass作者的原方法。
//定時器先執行 銷毀實例var Marquee1 = new Marquee(["div1", "Content"])function Marquee_everyTime() { Marquee1.Destroy(); //銷毀實例應用 GetMarqueeInfo(); //然后ajax采集需要的信息數據。}//ajax方法function GetMarqueeInfo() { LG.ajax({ type: 'AjaxOther', method: 'GetMarqueeInfo', success: function (data, tipsContent) { $("#Content").html(""); $("#Content").html(tipsContent); //大容器| 小容器 |滾動的方向 |滾動的速度 | 大容器的寬度 | 大容器的高度 | 滾動休息時間 | 滾動休息時間 | 滾動結束時間 Marquee1.Direction = 2; Marquee1.Step = 0.4; Marquee1.Width = 640; Marquee1.Height = 30; Marquee1.Timer = 20; Marquee1.DelayTime = 4000; Marquee1.WaitTime = 3000; Marquee1.ScrollStep = 320; Marquee1.Start(); }, error: function () { LG.tip('信息加載失敗.'); } });}