It depends on what is causing my loss of motivation. Usually, it's not the project itself, but some external factor... something else going on in my life that affects my mood or thinking. But, sometimes it is actually the project. Something complex within it. Something feature request that doesn't quite "fit" or has implications that are challenging to reconcile. In that case, I will either work on something else, or I will do something else, completely unrelated to coding and let my mind work through the issue. On a rare occasion, I'll take a little time off to "refuel". There IS such a thing as coder's block, unfortunately.
I agree with Joe, sometimes all you need is to just walk away from the project for awhile and do something else completely. Whether that's going for a walk, or just grabbing a cup of coffee and coming back to it in an hour.
It can also depend on what is causing the loss of motivation. For me, lots of times it's because I'm trying to take on too much at once and realizing how much I'm getting myself into. What helps me in this situation is picking either a single feature / bug and breaking it down into smaller, achievable steps. Taking those on one at a time then becomes rewarding and you realize you are making progress again.
Another thing that I find myself getting lost in is being unsure of where to take something next. If I'm working on a side-project and I don't know if a feature is actually worth developing or if my scope is at all what it needs to be, it can be really difficult to continue if you can't find out the answers on your own. What I do in these situations is reach out to other individuals to bounce ideas off of. Places like reddit, webdev slack channels, language specific forums, etc. are all excellent places to ask for feedback and criticism.
The above is all in the context (to me) of personal projects. If you're finding that you're becoming burnt out or demotivated on a project at work, ask your higher-ups if you can swap projects/tasks for a bit.
TL;DR it all comes down to you finding out what is demotivating you, and taking the right steps to get your mind back in the right place :)
TheSheriff
Co-Founder, Founder, Entrepreneur & Problem Solver
I think this hashnode question I asked may help :)