The stock market has been on a wild rollercoaster ride over the past few months. After falling by more than 10% between July and late October, the S&P 500 has nearly recovered those losses completely in the last two weeks.

These stomach-churning ups and downs can be difficult for investors, especially if you're trying to decide whether to invest now or wait.

While there's no single correct way to invest, there's one mistake you may want to avoid in times like these: Waiting it out to see what happens.

Why waiting too long to invest can be risky

In theory, it may make sense to hold off on investing right now. Nobody can say for certain where stock prices are headed, and there's a chance they could drop as quickly as they surged. If that happens, investing right now may seem like a bad move. But there's also a chance that the market could continue its upward trajectory.

So what's the right thing to do? Keep investing, but maintain a long-term outlook.

Gold figurines of bear and bull.

Image source: Getty Images.

In the short term, the market will always be volatile to a degree. If you're waiting for the perfect time to buy, then, you'll end up waiting forever. Over many years, though, the market is incredibly consistent. If you keep investing regularly regardless of what stock prices are doing, you can still earn a lot of money over time.

For example, say you decided to invest in an S&P 500 index fund in February 2009 -- just a few weeks before the market bottomed out amid the Great Recession.

At the time, that may have seemed like one of the worst possible moments to invest, and your investment would have almost immediately lost value. However, by January 2010, you'd still have earned returns of more than 35%.

^SPX Chart

^SPX data by YCharts

On the other hand, say you had waited until September to invest. By that point, the market was already well into recovery mode, despite still experiencing some hiccups here and there. While that may have seemed like the safer strategy at the time, by January 2010, you'd only have earned returns of around 12%.

Over the long haul, good investments are likely to see positive returns -- even if they're extremely volatile in the near term. Rather than waiting for just the right time to buy, then, it's often smarter to keep investing and stay focused on the future.

One important caveat

Investing consistently through all the market's ups and downs is key to building long-term wealth. However, it's equally important to invest in the right places.

Plenty of stocks can thrive when the market is doing well, but only strong companies will be able to pull through tough times. Those are the stocks you want in your portfolio to ensure your investments survive over the long term.

The strongest stocks come from companies with solid underlying business fundamentals, such as healthy financials, a competent leadership team, and a competitive advantage in the industry. These companies may still take a hit during periods of volatility, but they're far more likely to recover and go on to see long-term growth.

The more of these stocks you have in your portfolio, the better your chances of earning positive returns over time. And by investing consistently despite short-term volatility, you can ensure you're doing everything possible to maximize your earnings in the stock market.