Fix a bug about writing data to file; logic of waiting seconds