This can happen if it gets too hot.
The inside of a computer (particualerly a laptop) can get very hot and some components (like the CPU) will just "stop working" if they get too hot.
If it is laptop dont use it on top of a cushion, pillow, mattress on a bed or similar item as air cant circulate underneath. Use it on a hard surface or even one of those laptop cooling devices.
If a desktop PC make sure there is plenty of room around it so cool air can be sucked in.
Over time the fans and air vents can clog up with dust and fluff, stopping air getting in and out so maybe use a hair drier to gently blow out any air vents (maybe do this outside) and brush it with a soft brush.
If it still switches off after doing this you may have a hardware problem like a faulty power supply.