setTimeout triggering immediately

803 views
Skip to first unread message

David L

unread,
Jul 6, 2012, 2:36:44 PM7/6/12
to chromium-...@chromium.org
I am trying to use a setTimeout function in my Options page to show a "saved" box and then have it go away after a few seconds. I've tried the below code and nothing ever happens.
 
function saveOptions() {
// bunch of code here to save the settings
setTimeout( "alert('saved')", 5000); // attempt 1
setTimeout( function() { alert('saved'); }, 5000); // attempt 2
}

David L

unread,
Jul 6, 2012, 2:45:39 PM7/6/12
to chromium-...@chromium.org
Sorry, title should be "setTimeout not triggering"

Abraham Williams

unread,
Jul 6, 2012, 2:47:56 PM7/6/12
to David L, chromium-...@chromium.org
You are actually executing saveOptions() right? Just pasting it into dev tools console triggers alerts as expected. Are you seeing any errors in the JS console?

Abraham
--
Abraham Williams | abrah.am | abraham+
@abraham | github.com/abraham | blog.abrah.am
This email is: [ ] shareable [x] ask first [ ] private.



On Fri, Jul 6, 2012 at 11:45 AM, David L <ree...@gmail.com> wrote:
Sorry, title should be "setTimeout not triggering"

--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msg/chromium-extensions/-/Ey2c0sCxgRgJ.

To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.
For more options, visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/?hl=en.

David L

unread,
Jul 6, 2012, 3:03:12 PM7/6/12
to chromium-...@chromium.org, David L
I actually get the same thing, it worked for me in the dev console also. No errors at all in the console. I think I have found the issue by viewing the Timeline but not sure why it's happening. It appears that the options.html page is actually reloading after I hit the save button. Here is more code, maybe I'm missing something or maybe setting localStorage makes the page reload, not sure.
 
The Javascript: 
function save_options() {
 //save settings code here
 localStorage["setting1"] = "value";
  
 setTimeout( function() { alert('saved'); }, 5000);
}
document.getElementById("save_button").addEventListener("click", save_options, false);
 
 
The HTML Button: 
<button id="save_button"> Save </button> 

Abraham Williams

unread,
Jul 6, 2012, 4:53:00 PM7/6/12
to David L, chromium-...@chromium.org
I assume the button is part of a form and causing the page to "submit" to itself. You should be able to cancel the form submit.


Abraham
--
Abraham Williams | abrah.am | abraham+
@abraham | github.com/abraham | blog.abrah.am
This email is: [ ] shareable [x] ask first [ ] private.



--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.

David L

unread,
Jul 6, 2012, 5:03:14 PM7/6/12
to chromium-...@chromium.org, David L

Ya, someone emailed me directly and told me about that. All fixed now. Here is the code that fixed the issue. I appreciated everyones help on this :)

 
document.getElementById('settings').addEventListener('submit', function(e) { e.preventDefault(); console.log('capture submit'); }, false); 

Joshua Woodward

unread,
Jul 6, 2012, 3:07:01 PM7/6/12
to David L, chromium-...@chromium.org
My extension has an options page, with a save button, i just uploaded the code to github, you can check it out there.

The difference is, I have a div that I place a "saved" message in, not an alert. However I am still using a setTimeout wrapped in a save function to do it.


extension - 

Code on GitHub-
https://github.com/joshuawoodward/plus1-Coins 

On Fri, Jul 6, 2012 at 12:03 PM, David L <ree...@gmail.com> wrote:

--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.

To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.
For more options, visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/?hl=en.



--


Joshua Woodward

Joshua Woodward

unread,
Jul 6, 2012, 3:46:21 PM7/6/12
to David L, chromium-...@chromium.org
Found your bug

The page is not reloading, however the form is being submitted and the default action is itself, so it appears to be reloading

You need to capture the submit event, and have it do nothing, or remove the form tags, I suggest capture the event ;)

the following code will capture the form submit for you.

document.getElementById('settings').addEventListener('submit', function(e) {e.preventDefault(); console.log('capture submit');},false); 

Reply all
Reply to author
Forward
0 new messages