0

I have a drop-down list for categories. I created a popup for new category. When I hit submit the form, a new category is stored in a DB table, which will show on the drop-down list for categories. However, when I submit the form, it closes and reload so quick that the new category is not inserted into the DB table yet. How do I set 5 seconds to reload the parent page after the popup window closes.

My code:

function reloadIt() {
opener.location.reload(true);
self.close();
}

Thanks.

Assafs
  • 3,257
  • 4
  • 26
  • 39
Jenny Tran
  • 251
  • 5
  • 16

2 Answers2

2

You can use setTimeout to "schedule" a function for later:

setTimeout(() => { /*...*/ }, 5000);

edit:

Here's a more fleshed out version, sans arrow syntax:

setTimeout(function() {
  opener.location.reload(true);
  self.close();
}, 5000);
shabs
  • 718
  • 3
  • 10
  • 2
    No, it's the exact opposite. Or I'm going crazy. Googling to determine sanity-state. *(edit: Was responding to now-deleted comment.)* – shabs Aug 22 '17 at 17:45
  • 1
    No, you're right @shabs, this is exactly what the asker was looking for. – petetetete Aug 22 '17 at 17:46
  • @JennyTran, replace "`/*...*/`" with the logic you want to execute. Keep in mind that arrow syntax requires ES6 support or transpilation. – shabs Aug 22 '17 at 17:49
  • I tried that. When I clicked the popup window and started filling out the form, it closed just 5 seconds even though I have not submitted it. I like to submit the form by myself, then, it closes popup, then the parent refresh to get updated info after 5 seconds. – Jenny Tran Aug 22 '17 at 17:57
  • Move the snippet into your submit handler. – shabs Aug 22 '17 at 17:58
1
function reloadIt() {
    setTimeout(function () {
        opener.location.reload(true);
        self.close();
    }, 5000);
}
jANVI
  • 718
  • 3
  • 12