Knicks guard Tim Hardaway Jr. drives around Jazz guard Ricky...

Knicks guard Tim Hardaway Jr. drives around Jazz guard Ricky Rubio in the first half on Friday, Jan. 19, 2018, in Salt Lake City. Credit: AP/Rick Bowmer

SALT LAKE CITY — Jeff Hornacek was nervous. Not about his job security, but about this game.

The Knicks appeared to be suffering another meltdown, allowing a 10-point lead with two minutes remaining to dwindle to three. With a chance to tie the score, the Jazz then attempted three open three-pointers with less than 30 seconds left — all on the same possession — and missed all three.

Hornacek said the Knicks were lucky. They also were elated and relieved after escaping Utah with a 117-115 victory as speculation about their coach’s future became a topic.

The Knicks (21-25) had a chance to put the game away with 32.5 seconds left, but Tim Hardaway Jr. — who shot 6-for-7 from three-point range and scored 31 points — missed two foul shots. It led to that fateful Utah possession.

Rookie Donovan Mitchell missed an off-balance three-pointer, but Utah got the rebound. Joe Johnson missed a corner three-pointer, but Utah got the rebound. Then Joe Ingles short-armed a three-pointer from the opposite corner.

“I thought all three were going to go in when they left their hands,” Hornacek said. “We got to come up with those rebounds. We’re lucky they missed them.”

The Knicks (21-25) finally rebounded and Hardaway was fouled with 9.3 seconds to go. This time he made both to give the Knicks a five-point lead, enough to overcome Mitchell’s three-pointer with one second left. They moved to 6-16 on the road after winning the second game of this six-game western trip.

Knicks forward Kristaps Porzingis, rear, defends against Utah Jazz center...

Knicks forward Kristaps Porzingis, rear, defends against Utah Jazz center Rudy Gobert during the first half on Friday, Jan. 19, 2018, in Salt Lake City. Credit: AP / Rick Bowmer

“I definitely wanted to redeem myself,” Hardaway said.

The Knicks did, too, after one of their worst losses of the season on Wednesday night in Memphis.

“This was a must-win,” said Hardaway, who shot 11-for-17 overall. “Thank God we came up and got the win. They had three wide-open looks that they could have made. Luckily enough, we came out with the victory.”

It was one of the best games of the season for Hardaway, whose four-point play and driving dunk gave the Knicks a 109-100 lead with 3:21 remaining. He returned to the starting lineup in his fourth game back from a stress injury in his lower left leg.

Courtney Lee and Kristaps Porzingis each had 18 points. With rookie Frank Ntilikina struggling to make an impact, Hornacek gave newly signed Trey Burke crunch-time minutes, and he responded with seven points and two assists.

Utah center Rudy Gobert returned after missing the past 15 games and had 23 points and 14 rebounds. Rodney Hood had 18 points and Mitchell and Ricky Rubio each scored 17 for Utah (18-27).

The Knicks appeared to be in good shape after Porzingis’ three-pointer gave them a 113-103 lead with 2:12 left. But when Rubio missed the second of two free throws, Johnson rebounded it, was fouled on a three-pointer and sank all three attempts. Mitchell then scored on a drive to make it 113-109 with 1:30 to go.

Michael Beasley’s jumper put the Knicks up by six, but Gobert’s three-point play brought the Jazz within 115-112 with 1:07 to go.

“I liked the effort,” said Hornacek, who played for Utah for seven seasons. “We just have to continue to be smart. We got to continue to be stronger, continue to be aggressive.”

Third quarters have been an issue for the Knicks, but Lee scored 11 points in the first 5:09 and 13 overall. Hardaway scored 11 in the quarter, including a one-man 8-0 run in the final 54.3 seconds that gave the Knicks an 85-78 lead.

“We came out much better,” Porzingis said. “The third quarter was huge for us. We kept playing.”

Newsday LogoSUBSCRIBEUnlimited Digital AccessOnly 25¢for 5 months
ACT NOWSALE ENDS SOON | CANCEL ANYTIME