It is probably when in particularly dark conditions, or when you were using an autofocus point that wasn't a cross type, or both.
The thing you are describing the focus doing is called 'hunting', and I imagine, unless there is a fault with the lens (didn't happen to drop it did you?

), then it is probably what I mentioned above. It sounds complicated but it isn't really. If you want / need more info then just ask and I, or another member will provide