How to turn an electromagnet on and off?
You really have two problems: generating a time interval, and driving an inductive load.

For the timing you could use an analog circuit such as a 555 timer, or you could use a micro-controller running a simple program.  Either can use a variable resistor to set the value - the 555 uses it to control the time constant of charging a capacitor, the micro-controller can read it via an ADC or similarly in a capacitor time constant measurement if the micro doesn't have an ADC.  The analog circuit will be simpler to document and understand, and can be developed with fewer tools, while the micro-controller will be more versatile for variations on the task and potentially more accurate if a crystal or resonator is used as the clock reference.

The common way to drive the coil is likely with a general purpose NPN transistor on the ground side of the supply circuit - emitter to ground, collector to the coil, and a resistor feeding the base from the timing circuit.  It would be a good idea to place a fast switching diode "backwards" across the coil to handle the inductive kick which occurs when it is switched off.  You don't mention the specs of the electromagnet, so it's hard to recommend a specific transistor.