-_-#setTimeout / setInterval

你真的了解setTimeout和setInterval吗?

 

function sleep(milliSeconds) {

    var startTime = new Date().getTime()

    while (new Date().getTime() < startTime + milliSeconds) {}

}



var i = 0

setInterval(function() {

    console.log(+new Date(), ++i)

    sleep(4000)

}, 2000)

/*

间隔4s左右

1395043279715 1

1395043283750 2

1395043287753 3

1395043291757 4

1395043295763 5

1395043299763 6 

*/



/*setTimeout(function() {

    console.log(+new Date(), ++i)

    sleep(4000)

    setTimeout(arguments.callee, 2000)

}, 2000)

*/

/*

间隔6s左右

1395042762150 1

1395042768158 2

1395042774169 3

1395042780177 4

1395042786177 5

1395042792177 6

*/

 

你可能感兴趣的:(SetInterval)