In the vast majority of cases, you can charge a phone during a thunderstorm, and everything will be just fine. That said, any expert in the fields of electricity, telecommunications, and construction is likely to tell you that there is still a chance your phone could be damaged. The general recommendation is to unplug.
Regardless if it’s your phone, tablet, or laptop charger, they all have the same reason for going off and on. Typically, on-again-off-again charging happens because of a poor flow of electricity. But it can also be tied to your power port being dirty, a faulty charger or power source, or maybe it is your battery.