JavaScript setTimeout function not executing as expected
I'm driving myself crazy here trying to figure out why this script is no longer executing as expected (it was working great yesterday, but I made a few minor changes and didn't back it up...now I can't figure out what the problem is).
<form method="POST" name="inventory_check" id="inventory_check">
<input type="text" name="stock_part" id="part_input">
<input type="hidden" name="site" value="2">
<input type="submit" value="Check Stock" id="submit">
</form>
<?php
if(isset($_POST['stock_part'])) {
$part = strtoupper($_GET['stock_part']);
$site = $_POST['site'];
$filename = 'system/inventory/'.$part.'.txt';
if(file_exists($filename)) {
$handle = fopen($filename, 'r');
$content = fread($handle, filesize($filename));
$content = trim($content, '&l0O(10U');
$content = trim($content, 'E');
fclose($handle);
date_default_timezone_set('UTC');
$mod_date = (filemtime($filename));
$current_d开发者_JS百科ate = time();
$subtract_date = ($current_date - $mod_date);
$subtract_date = ($subtract_date/3600);
if ($subtract_date <= 12) {
$stock = number_format($content);
echo '<script>document.inventory_check.style.display="visible";</script>';
echo '<div id="stock_results">There are '.$stock.' '.$part.' in stock.</div>';
}
else {
echo 'File too old.';
}
}
else {
echo '<iframe src="http://example.com/inventory_check.php?part='.$part.'&site='.$site.'" height="50" width="150"></iframe>';
echo '<script>document.inventory_check.style.display="none";</script>';
echo '<div align="center"><img src="http://www.aifittingsproto.com/images/load.gif"><br /><br />Searching</div>';
echo '<script>setTimeOut("recheck()", 2000);</script>';
}
}
?>
<script>
function recheck() {
document.inventory_check.stock_part.value="<?php echo $part ?>";
document.inventory_check.site.value="<?php echo $site ?>";
document.inventory_check.submit();
}
</script>
Basically this checks stock of a certain item on our internal servers (different domain than web server). What I'm trying to accomplish is the user keys in the part number...first it checks if the ftp-ed file exists on our server..if so, it checks the time it checks if its fresher than 12hrs, and then displays it if so. If not, it opens an iframe and sends the variables to our internal server for processing, which all works as expected. My issue is, I need to be able to have it recheck if the file exists after variables are passed through the iframe. I attempted to set this up by resending the variables and submitting the form every 2 seconds. Looking at the source, all the variables are populating as expected, etc. but it is not looping through and resubmitting the form. Any suggestions why this is failing, or a better approach? Thanks in advance.
You should try replacing setTimeout("window.reload()", 2000)
with setTimeout("window.reload", 2000)
. My understanding is that you need to pass functions without the brackets for timeouts.
精彩评论