let rid;//the request animation frame id
let track = document.getElementById("track"),
trackLength = track.getTotalLength(),//the total length of the track
dur = 15000; //duration of one loop of track, in ms
let lastTime = 0;//last time when the rect has stopped
let flag = false;
let interval = 5000;
function update(time) {
rid = requestAnimationFrame(update);
// to stop the rect every 5 seconds
var deltaT = time%interval;
//during 1 second
if (deltaT < interval/2){
flag = false;
var t = ((deltaT + lastTime) % dur) / dur, // position in repeat cycle
distance, // distance along the path for this rect
point, // SVGPoint for that distance
point2; // SVGPoint for a slightly different distance
distance = trackLength * (t % 1);
point = track.getPointAtLength(distance);
point2 = track.getPointAtLength((distance + 2) % trackLength);
angle = Math.atan2(point.y - point2.y, point.x - point2.x);
rect.setAttribute(
"transform",
"translate(" +
[point.x, point.y] +
")" +
"rotate(" +
angle * 180 / Math.PI +
")"
);
}else{if(flag==false){lastTime += interval/2; flag = true;}}
}
rid = requestAnimationFrame(update);
svg{border:1px solid;}
path{fill:none; stroke:black;}
<svg viewBox = "0 0 150 200" width="200">
<path id="track" d="M70,30 Q70,20 80,20L120,20 Q130,20 130,30L130,170.000 Q130,180 120,180L30,180 Q20,180 20,170L20,130 Q20,120 30,120L60,120 Q70,120 70,110Z" />
<polygon id="rect" points="0,0 -10,0 -10,-10 0,-10 0,0" style="fill: #ff0000;"/>
</svg>