这是一个带有回调和间隔的实用程序对象,您可以使用它来启动和停止间隔。
//simple utility object to start and stop an interval
var IntervalUtil = function(functionCall, interval){
var intervalObj = 0,
INTERVAL = interval;
var callback = functionCall;
function startTimer(){
console.log('start timer', intervalObj)
if(intervalObj === 0){
intervalObj = setInterval(callback, INTERVAL)
}
}
function stopTimer(){
clearInterval(intervalObj);
intervalObj = 0;
console.log('timer stopped', intervalObj);
}
return {
startTimer : startTimer,
stopTimer : stopTimer
}
};
*你会这样使用它 *
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="author" content=""/>
<title>Timer : setInterval()</title>
<style type="text/css">
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
color:#000;
line-height:1.3em;
font: inherit;
vertical-align: middle;
font-family:arial;
font-size:12px;
outline:none;
}
a{
outline:none;
text-decoration:none;
color:#145486;
}
a span{
color:#145486;
}
h1{
font-weight:normal;
font-size:32px;
}
body{
background:#efefef;
text-align:center;
}
.content{
width:400px;
padding:20px;
margin:10px auto;
background:#fff;
border:solid 1px #ebebeb;
text-align:left;
}
#toggleTimer{
color:#fff;
font-size:10px;
padding:2px 7px;
display:inline-block;
background:#357cb7;
border:solid 1px #0c8af4;
}
#toggleTimer.running{
background:#f14621;
border:solid 1px #ff4c00;
}
#output{
padding:7px;
font-size:10px;
border:dashed 1px #ebebeb;
background:#f8f8f8;
margin-left:10px;
display:inline-block;
}
em{
font-weight:bold;
}
</style>
</head>
<body>
<div class="content">
<h1>Set Interval Code</h1>
<a href="javascript:" id="toggleTimer" class="">Start Timer</a>
<span id="output"></span>
<br class="clear"/>
</div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var demo = new Demo();
});
var Demo = function(){
var self = this;
var START_TEXT = "Start Timer",
STOP_TEXT = "Stop Timer";
var $toggle = $('#toggleTimer'),
$output = $('#output');
var count = 0;
var intervalUtil;
function init(){
intervalUtil = new IntervalUtil(printMessage, 1000);
setClickHandler();
return self;
}
function setClickHandler(){
$toggle.click(toggleTimer)
}
function toggleTimer(){
$toggle.toggleClass('running');
if($toggle.hasClass('running')){
$toggle.text(STOP_TEXT);
intervalUtil.startTimer();
}else{
$toggle.text(START_TEXT);
intervalUtil.stopTimer();
}
}
function printMessage(){
$output.html("printMessage called <em>" + (count++) + "</em> times");
}
return init();
}
//simple utility object to start and stop an interval
var IntervalUtil = function(functionCall, interval){
var intervalObj = 0,
INTERVAL = interval;
var callback = functionCall;
function startTimer(){
console.log('start timer', intervalObj)
if(intervalObj === 0){
intervalObj = setInterval(callback, INTERVAL)
}
}
function stopTimer(){
clearInterval(intervalObj);
intervalObj = 0;
console.log('timer stopped', intervalObj);
}
return {
startTimer : startTimer,
stopTimer : stopTimer
}
};
</script>
</body>
</html>