JavaScript setTimeout 函数未按预期执行

发布于 2024-11-30 22:54:57 字数 2712 浏览 1 评论 0原文

我在这里让自己发疯,试图弄清楚为什么这个脚本不再按预期执行(昨天工作得很好,但我做了一些小的改变,并且没有备份它......现在我无法弄清楚找出问题是什么)。

    <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_date = 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>

基本上,这会检查我们内部服务器(与网络服务器不同的域)上特定商品的库存。我想要完成的是用户键入零件号...首先它检查 ftp-ed 文件是否存在于我们的服务器上...如果存在,它会检查时间是否比 12 小时更新,然后如果是则显示它。如果没有,它会打开一个 iframe 并将变量发送到我们的内部服务器进行处理,这一切都会按预期工作。我的问题是,我需要能够在变量通过 iframe 传递后重新检查文件是否存在。我尝试通过每 2 秒重新发送变量并提交表单来进行设置。查看源代码,所有变量都按预期填充等,但它没有循环并重新提交表单。有什么建议为什么会失败,或者有更好的方法吗?提前致谢。

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_date = 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

長街聽風 2024-12-07 22:54:57

您应该尝试将 setTimeout("window.reload()", 2000) 替换为 setTimeout("window.reload", 2000)。我的理解是,您需要传递不带超时括号的函数。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文