Skip to content

Commit

Permalink
learn
Browse files Browse the repository at this point in the history
  • Loading branch information
abstain23 committed May 29, 2020
1 parent 8a572c6 commit 37b556c
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 0 deletions.
Binary file added 浏览器渲染/async与defer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions 浏览器渲染/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.demo {
width: 100px;
height: 100px;
background-color: red;
/* position:absolute;
top: 0;
transform: translateZ(0); */
will-change: transfrom;
}
</style>
</head>
<body>
<div class="demo"></div>
<div style="width: 200px;height: 200px; margin-top: 200px;background-color: green;"></div>
</body>
</html>
20 changes: 20 additions & 0 deletions 防抖和节流/debounce.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const input = document.getElementById('input')

input.addEventListener('keyup', debounce(ajax, 300))


function ajax(e) {
console.log(e.target.value)
console.log(this)
console.log('ajax 请求')
}

function debounce(fn,delay) {
let timer
return function(...args) {
clearTimeout(timer)
timer = setTimeout(() => {
fn.apply(this, args)
}, delay)
}
}
15 changes: 15 additions & 0 deletions 防抖和节流/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
函数防抖:<input type="text" name="" id="input">
函数节流:<input type="text" name="" id="input2">

<script src="./debounce.js"></script>
<script src="./throttle.js"></script>
</body>
</html>
31 changes: 31 additions & 0 deletions 防抖和节流/throttle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const input2 = document.getElementById('input2')

input2.addEventListener('keyup', debounce(ajax2, 1000))

function ajax2(e) {
console.log(e.target.value)
console.log(this)
console.log('ajax 请求')
}

function throttle(fn, delay) {
let timer
return function(...args) {
if(timer) return
timer = setTimeout(() => {
fn.apply(this, args)
timer = null
}, delay)
}
}

function throttle2(fn, delay) {
let pre = 0
return function (...args) {
let now = new Date()
if(now - pre > delay) {
fn.apply(this, ...args)
pre = now
}
}
}

0 comments on commit 37b556c

Please sign in to comment.