Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
web-framework-temp
/
vue-cli3-vt-template-master
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
2a145a18
authored
2022-09-01 15:45:30 +0800
by
simon
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
1、路由兼容name写法
2、utils修复防抖/节流函数
1 parent
4d62f5c3
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
60 additions
and
53 deletions
src/utils/utils.js
src/utils/utils.js
View file @
2a145a1
...
...
@@ -199,64 +199,71 @@ export function formatDate(date, fmt) {
/**
* @desc 函数防抖
* @param func 函数
* @param wait 延迟执行毫秒数
* @param immediate true 表立即执行,false 表非立即执行
*/
export
function
debounce
(
func
,
wait
,
immediate
)
{
let
timeout
;
return
function
()
{
let
context
=
this
;
let
args
=
arguments
;
if
(
timeout
)
clearTimeout
(
timeout
);
if
(
immediate
)
{
var
callNow
=
!
timeout
;
timeout
=
setTimeout
(()
=>
{
timeout
=
null
;
},
wait
)
if
(
callNow
)
func
.
apply
(
context
,
args
)
}
else
{
timeout
=
setTimeout
(
function
()
{
func
.
apply
(
context
,
args
)
},
wait
);
}
}
}
/**
* @desc 函数
节流
* @desc 函数
防抖
* @param func 函数
* @param wait 延迟执行毫秒数
* @param type 1 表时间戳版,2 表定时器版
* 时间戳版的函数触发是在时间段内开始的时候,而定时器版的函数触发是在时间段内结束的时候。
* @param immediate true 表立即执行,false 表非立即执行
*/
export
function
throttle
(
func
,
wait
,
type
)
{
if
(
type
===
1
)
{
var
previous
=
0
;
}
else
if
(
type
===
2
)
{
var
timeout
;
}
return
function
()
{
let
context
=
this
;
let
args
=
arguments
;
if
(
type
===
1
)
{
let
now
=
Date
.
now
();
let
debounceTimeout
;
if
(
now
-
previous
>
wait
)
{
func
.
apply
(
context
,
args
);
previous
=
now
;
}
}
else
if
(
type
===
2
)
{
if
(
!
timeout
)
{
timeout
=
setTimeout
(()
=>
{
timeout
=
null
;
func
.
apply
(
context
,
args
)
},
wait
)
}
}
}
}
\ No newline at end of file
export
function
debounce
(
func
,
wait
,
immediate
)
{
return
function
()
{
let
context
=
this
;
let
args
=
arguments
;
if
(
debounceTimeout
)
clearTimeout
(
debounceTimeout
);
if
(
immediate
)
{
var
callNow
=
!
debounceTimeout
;
debounceTimeout
=
setTimeout
(()
=>
{
debounceTimeout
=
null
;
},
wait
)
if
(
callNow
)
func
.
apply
(
context
,
args
)
}
else
{
debounceTimeout
=
setTimeout
(
function
()
{
func
.
apply
(
context
,
args
)
},
wait
);
}
}();
}
/**
* @desc 函数节流
* @param func 函数
* @param wait 延迟执行毫秒数
* @param type 1 表时间戳版,2 表定时器版
* 时间戳版的函数触发是在时间段内开始的时候,而定时器版的函数触发是在时间段内结束的时候。
*/
let
throttleTimeout
;
let
throttlePrevious
;
export
function
throttle
(
func
,
wait
,
type
)
{
if
(
type
===
1
)
{
throttlePrevious
=
0
;
}
else
if
(
type
===
2
)
{
throttleTimeout
=
0
;
}
return
function
()
{
let
context
=
this
;
let
args
=
arguments
;
if
(
type
===
1
)
{
let
now
=
Date
.
now
();
if
(
now
-
throttlePrevious
>
wait
)
{
func
.
apply
(
context
,
args
);
throttlePrevious
=
now
;
}
}
else
if
(
type
===
2
)
{
if
(
!
throttleTimeout
)
{
throttleTimeout
=
setTimeout
(()
=>
{
throttleTimeout
=
null
;
func
.
apply
(
context
,
args
)
},
wait
)
}
}
}();
}
\ No newline at end of file
...
...
Please
register
or
sign in
to post a comment