How do I delay my function (beginUpload()) from executing when it is called? I tried the function delay(), but the function still executes instantly after my form is submitted. I 开发者_JS百科don't want the function to execute over and over on an interval. I just want to delay its execution for a period of time. Anyone have any ideas?
<form action="testing12345.php" target="progressFrame" method="post" id="uploadform" enctype="multipart/form-data" onsubmit="beginUpload().delay(8000);">
Take it out of your DOM and use setTimeout:
HTML:
<form action="testing12345.php" target="progressFrame" method="post" id="uploadform" enctype="multipart/form-data">
JQuery:
$('#uploadForm').submit(function(event){
event.preventDefault(); // Prevent the actual form submission so the page remains.
var sub = setTimeout(function(){
beginUpload();
},8000);
});
This should do the trick:
setTimeout(beginUpload, 8000);return false;
You need to return false to stop the browser submitting the form.
The JavaScript setTimeout function is what you are after. You can call it in the form tag itself e.g.
<form action="testing12345.php" target="progressFrame" method="post" id="uploadform" enctype="multipart/form-data" onsubmit="setTimeout(beginUpload(), 2000);">
where 2000 = 2 seconds
or you could use it a similar way inside the beginUpload method
.delay
will not prevent the submit from happening.
onsubmit="setTimeout(beginUpload, 8000), return false;"
精彩评论