Repository navigation
Expand file tree
/
Copy pathspa.ts
More file actions
64 lines (59 loc) · 1.81 KB
/
Copy pathspa.ts
File metadata and controls
64 lines (59 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// @ts-ignore
import { location } from 'svelte-spa-router'
import { get } from 'svelte/store'
let >
location.subscribe(currentLocation => {
for (let onceListKey of Object.keys(onceList))
if (onceListKey != currentLocation) delete onceList[onceListKey]
})
/**
* @description
* This function allows the desired function
* to operate only once within the page.
* (This feature is applicable until a page move occurs.)
*/
export const () => void, identifier: string) => {
let registeredLocation = get(location)
if (
typeof onceList[registeredLocation] == 'undefined' ||
typeof onceList[registeredLocation][identifier] == 'undefined'
) {
if (typeof onceList[registeredLocation] == 'undefined')
onceList[registeredLocation] = {}
onceList[registeredLocation][identifier] = true
callback()
}
}
/**
* @description
* This function will run the function repeatedly
* only when no other page has been moved
* from the first scheduled page.
*/
export const setInterval = (handler, timeout: number) => {
let registeredLocation = get(location)
let intervalHandler = setInterval(() => {
if (get(location) != registeredLocation) {
clearInterval(intervalHandler)
} else {
handler()
}
}, timeout)
return intervalHandler
}
/**
* @description
* This function runs a callback only if no other
* page has been moved from the first scheduled page.
*/
export const setTimeout = (handler, timeout: number) => {
let registeredLocation = get(location)
let timeoutHandler = setTimeout(() => {
if (get(location) != registeredLocation) {
clearTimeout(timeoutHandler)
} else {
handler()
}
}, timeout)
return timeoutHandler
}