Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions projects/ngx-page-scroll-core/src/lib/page-scroll-instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ export class PageScrollInstance {
/* Whether an interrupt listener is attached to the body or not */
public interruptListenersAttached = false;

/* References to the timer instance that is used to perform the scroll animation to be
/* Value of the requestAnimationFrameId that is used to perform the scroll animation to be
able to clear it on animation end*/
public timer: any = null;

public requestFrameId: number | null = null;

/**
* Private constructor, requires the properties assumed to be the bare minimum.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ export class PageScrollService {
pageScrollInstance.detachInterruptListeners();
}

if (pageScrollInstance.timer) {
if (pageScrollInstance.requestFrameId) {
// Clear/Stop the timer
clearInterval(pageScrollInstance.timer);
window.cancelAnimationFrame(pageScrollInstance.requestFrameId)
// Clear the reference to this timer
pageScrollInstance.timer = undefined;
pageScrollInstance.requestFrameId = null;
pageScrollInstance.fireEvent(!interrupted);

return true;
Expand Down Expand Up @@ -198,7 +198,14 @@ export class PageScrollService {
// .. and calculate the end time (when we need to finish at last)
pageScrollInstance.endTime = pageScrollInstance.startTime + pageScrollInstance.executionDuration;

pageScrollInstance.timer = setInterval((instance: PageScrollInstance) => {
pageScrollInstance.requestFrameId = window.requestAnimationFrame(this.updateScrollPostion(pageScrollInstance))
Copy link
Owner

@Nolanus Nolanus Feb 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The global window object shouldn't be access directly, rather the little bit more complicated angular way via document object, that we already got hold of at the pageScrollInstance config
https://stackoverflow.com/a/50023378/2225619 , https://stackoverflow.com/a/52620181/2225619)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, I changed it, now I am using pageScrollInstance.pageScrollOptions.document.defalutView instead of window


// Register the instance as running one
this.runningInstances.push(pageScrollInstance);
}

public updateScrollPostion(instance: PageScrollInstance){
return ()=>{
// Take the current time
const currentTime: number = new Date().getTime();

Expand Down Expand Up @@ -231,12 +238,10 @@ export class PageScrollService {
// (otherwise the event might arrive at "too early")
if (stopNow) {
this.stopInternal(false, instance);
} else{
window.requestAnimationFrame(this.updateScrollPostion(instance))
}

}, this.config._interval, pageScrollInstance);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the config _interval is not needed any longer to "customize" the animation/repaint, it should be removed

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have deleted everything related with config._interval


// Register the instance as running one
this.runningInstances.push(pageScrollInstance);
}
}

public scroll(options: PageScrollOptions): void {
Expand Down