240624
This commit is contained in:
		
							
								
								
									
										44
									
								
								6.6setTimeoutandsetInterval/t.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								6.6setTimeoutandsetInterval/t.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,44 @@ | ||||
| function sayHi(){ | ||||
|     console.log('Hello'); | ||||
| } | ||||
|  | ||||
| setTimeout(sayHi, 1000); | ||||
|  | ||||
| function sayHi2(phrase, who){ | ||||
|     console.log(phrase + ',' + who); | ||||
| } | ||||
| setTimeout(sayHi2, 100, "Hi", "John"); | ||||
|  | ||||
|  | ||||
| let timerId = setInterval(()=>console.log(('tick')), 1000); | ||||
|  | ||||
| setTimeout(() => {clearInterval(timerId); console.log('stop!')}, 5000); | ||||
|  | ||||
| timerId = setTimeout(function tick(){ | ||||
|     console.log('tick'); | ||||
|     timerId = setTimeout(tick, 1000); | ||||
| }, 1000) | ||||
|  | ||||
| function printNumbers(from, to){ | ||||
|     let cur = from | ||||
|  | ||||
|     let timerId = setInterval(() => { | ||||
|         console.log(cur) | ||||
|         if(cur == to){ | ||||
|             clearInterval(timerId); | ||||
|         } | ||||
|         cur += 1; | ||||
|     }, 1000); | ||||
| } | ||||
| function  printNumbers2(from, to){ | ||||
|     cur = from | ||||
|     setTimeout(function go() { | ||||
|         console.log(cur); | ||||
|         if(cur < to){ | ||||
|             setTimeout(go, 1000); | ||||
|         } | ||||
|         cur++; | ||||
|     }, 1000) | ||||
| } | ||||
| printNumbers(10, 15); | ||||
| printNumbers2(10, 15); | ||||
		Reference in New Issue
	
	Block a user