0

Why doesn't my JavaScript fire, the checkprogress form button works if I manually click it, but it doesn't automatically fire, my JavaScript is meant to fire 5 seconds after page is displayed

This is the end of the html page ...

<form action="/check_progress" name="check_progress">
    <p>
        <input type="submit" value="Check Progress">
    </p>
</form>
<form action="/pause">
    <p>
        <input type="submit" value="Pause">
    </p>
</form>
<script>function checkprogress() { setTimeout(5000); document.getElementsByName('check_progress').submit(); } </script></body>
</html>
Derrick
  • 3,669
  • 5
  • 35
  • 50
Paul Taylor
  • 13,411
  • 42
  • 184
  • 351
  • 1
    You should also look at the documentation for `setTimeout`. It needs to be **outside** your function, not inside it. – Quentin Dec 11 '17 at 13:58
  • you need to name the inputs in your form that you wish to submit automatically `name="key_name"` – WouldBeNerd Dec 11 '17 at 13:58
  • All Im trying to do is submit form to server after 5 seconds, server will then create new page with updated status. This works if I manually click on the Check Progress button but not if I rely on the Javascript, I dont atcually even want the Check Progress button I just want the request to be made to the server after five seconds. – Paul Taylor Dec 11 '17 at 14:02
  • You could add an event for page load then use a timeout to submit the form. I don't actually know JS well enough so this can probably be improved but does work. `document.addEventListener('DOMContentLoaded', function() { setTimeout(function() { document.check_progress.submit(); }, 5000); })` – jhine Dec 11 '17 at 14:03
  • @Quentin I dont understand why duplicate, form name is check_progress, function checkprogress refers to that name correctly ? – Paul Taylor Dec 11 '17 at 14:04
  • @PaulTaylor — Umm. Nothing in the duplicate question suggests that you are using the wrong *name*. Try reading the accepted answer again. – Quentin Dec 11 '17 at 14:05
  • @Quentin, I have chnaged so form now has id of check_progress and use document.getElementById(), and added an alert() at start of Javascript function but still nothing happens ! – Paul Taylor Dec 11 '17 at 14:11
  • "You should also look at the documentation for setTimeout. It needs to be outside your function, not inside it." (You never call the function!) – Quentin Dec 11 '17 at 14:12
  • @Quentin Ive removed it so now have function checkprogress() { alert('fred'); document.getElementById('check_progress').submit(); } but it doesnt even display the alert – Paul Taylor Dec 11 '17 at 14:15
  • @Jerry — Utter rubbish. There is no practical limit to the number of form elements you can have. – Quentin Dec 11 '17 at 14:17
  • Is that what is breaking the Javascript, because both forms seem to work fine and it makes sense to be rather than cramming different tasks into the same form – Paul Taylor Dec 11 '17 at 14:18
  • @PaulTaylor —You still aren't calling the function – Quentin Dec 11 '17 at 14:18
  • @Quentin I see what you mean now , how do call it, I tried this but didn't work - – Paul Taylor Dec 11 '17 at 14:22
  • That should work – Quentin Dec 11 '17 at 14:23
  • @Quentin now working, sorry had typo timout instread of timeout, thanks – Paul Taylor Dec 11 '17 at 14:43
  • @Quentin but what I really want to do is remove the checkprogress form and just have the javascript submit direct to server, how would I do this ? – Paul Taylor Dec 11 '17 at 14:45

0 Answers0