Jump to content
Search Community

I try to use clear function in timeline

hani1990 test
Moderator Tag

Recommended Posts

I got an error here:

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'totalTime')
    at Timeline.restart (gsap.min.js:10:20564)
    at tl.clear (confettiCuisine.js:118:10)
    at Socket.<anonymous> (confettiCuisine.js:145:16)
    at Emitter.emit (index.mjs:136:20)
    at Socket.emitEvent (socket.js:498:20)
    at Socket.onevent (socket.js:485:18)
    at Socket.onpacket (socket.js:455:22)
    at Emitter.emit (index.mjs:136:20)
    at manager.js:204:18

and this is the code :::
 


function bel(){
    const sound = new Audio('/sound/mixkit-fairy-message-notification-861.wav');
    sound.addEventListener('canplaythrough', ()=>{
        sound.play();
    })
}


let tl = gsap.timeline( {paused: true, repeat:2});
tl.play = function(){
    tl
      .to(leftBell, { duration: 0.15, css: { scale: 1.2, skewY: "1deg", skewX: "-1deg" }, ease: Power0.easeNone })
      .to(rightBell, { duration: 0.15, css: { scale: 1.2, skewY: "1deg", skewX: "-1deg" }, ease: Power0.easeNone })
      .to(bodyBell, { x: 0, transformOrigin: "50% 50%" })
      .to(bodyBell, {
        keyframes: [
          { x: -3, rotate: 10},
          { x: 1.5, rotate: -10},
          { x: -1.5, rotate: 6.0 },
          { x: 1.5, rotate: -4.4},
          { x: -1.5, rotate: 2.2 }
        ],
        duration:0.4,
        ease: Power0.easeNone
      });
}
tl.clear = function (){
    this.kill();
    this.restart();
}


// const submit = () => {
function submit() {
    form.addEventListener('submit', (e) => {
        e.preventDefault();
        socket.emit('message', {
            content: chatInput.value,
            userName: Username.value,
            id: Id.value
        });
        chatInput.value = "";
        return false;
    });

    // what i get from the server
    socket.on('message', (msg) => {
        
        if(window.location.pathname !=="/chat"){
            displayMessage(msg);
            bel();
            tl.play();
        }else{
            displayMessage(msg);
            tl.clear();
        }
        chat.scrollTop = chat.scrollHeight;
    })

    socket.on('load all messages', (data) => {
        data.forEach(message => {
            displayMessage(message);
        })
        chat.scrollTop = chat.scrollHeight;
    })

}

 

Link to comment
Share on other sites

Without a minimal demo, it's very difficult to troubleshoot; the issue could be caused by CSS, markup, a third party library, a 3rd party script, etc. Would you please provide a very simple CodePen or Stackblitz that illustrates the issue? 

 

Please don't include your whole project. Just some colored <div> elements and the GSAP code is best. See if you can recreate the issue with as few dependancies as possible. Start minimal and then incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, at least we have a reduced test case which greatly increases your chances of getting a relevant answer.

 

See the Pen aYYOdN by GreenSock (@GreenSock) on CodePen

that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

 

Using a framework/library like React, Vue, Next, etc.? 

CodePen isn't always ideal for these tools, so here are some Stackblitz starter templates that you can fork and import the gsap-trial NPM package for using any of the bonus plugins: 

 

Please share the StackBlitz link directly to the file in question (where you've put the GSAP code) so we don't need to hunt through all the files. 

 

Once we see an isolated demo, we'll do our best to jump in and help with your GSAP-specific questions. 

Link to comment
Share on other sites

This looks very strange and problematic to me: 

tl.clear = function (){
    this.kill();
    this.restart();
}

Why are you re-defining the built-in clear() function? That's typically a very bad idea. Did you know that a timeline's kill() method itself calls clear() internally? 

 

And you don't need to define a css:{} object like that. I'd strongly recommend using the more modern ease syntax, like ease: "none" instead of ease: Power2.easeNone

 

If you still need help, please make sure you include a minimal demo that clearly illustrates the issue and we'd be glad to take a peek. 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...