setInterval("f()",1000) 每隔1秒就执行一次f()
clearInterval 关闭clearInterval
setTimeout("f()",1000) 1秒后执行f(),只执行一次
clearTimeout 关闭setTimeout
1.简单版应用html代码
<html>
<head>
<script type="text/javascript">
var c=0
var t
function timedCount() {
document.getElementById('txt').value=c
c=c+1
t=setTimeout("timedCount()",1000)
}
function stopCount() {
clearTimeout(t)
}
</script>
</head>
<body>
<form>
<input type="button" value="Start count!" onClick="timedCount()">
<input type="text" id="txt">
<input type="button" value="Stop count!" onClick="stopCount()">
</form>
</body>
</html>
2.文字上下翻滚html代码(无缺陷)
<script type="text/javascript">
$(document).ready(function(){
$n = $("#Tscroll li").length; // 1.给ul一个高度
$("#Tscroll ul").css("height",$n*35)
$("#Tscroll").mouseenter(function(){ // 不推荐用mouseover
clearTimeout(ht); // 2.停止ht;
});
$("#Tscroll").mouseleave(function(){ // 不推荐用mouseout,
Tscroll(); // 3.滚动 Tscroll
});
});
function Tscroll(){ // 4.定义 Tscroll
var f = $("#Tscroll li").length;
var n = parseInt($("#Tscroll ul").css("top"));
if(n<=-f*35+35){
$("#Tscroll ul").animate({top:0});
}else{
$("#Tscroll ul").animate({top:n-35});
}
ht = setTimeout("Tscroll()",1000); // 5.每个一秒执行 Tscroll(); 整个函数组合起来就是一个无限循环
}
Tscroll();
</script>
来源:https://www.cnblogs.com/wesky/p/3898941.html