JavaScript setTimeout 不会消亡。

发布于 2024-12-20 20:04:35 字数 883 浏览 1 评论 0原文

我制作了一个照片库,并由 AJAX 驱动。下面是相关的代码片段。

    if (isset($_POST['stopss'])){
    echo'<script type="text/javascript">
    clearTimeout(setss);
    </script>';
    }

    if (isset($_POST['startss'])){
    ?>
    <script type="text/javascript">
    var setss = window.setTimeout('delayer(\''+src+'\',\''+dir+'\',\''+title+'\',\''+width+'\',\''+height+'\',\''+img+'\')',5000);
    </script>
    <?
    }

延迟器函数传递新模态框窗口的参数(新的 AJAX 请求)。 我所看到的问题。当用户单击幻灯片按钮时,超时开始。 即使他们按下“停止幻灯片放映”按钮(这会重新加载 AJAX 页面),计时器仍在运行(在后台)并且在clearTimeout(setss) 中,sets 不再可用(因为新的 AJAX 页面请求),因此clearTimeout无法清除。

有什么办法可以消除后台运行的超时吗?即使 modalbox 窗口关闭,5 秒后它也会打开并愉快地继续播放幻灯片。

顺便说一句,这是模态框调用。 Modalbox.show('../b-gal-scripts/ajaxgal.php',{title:标题,overlayOpacity:.1,宽度:宽度,高度:高度,方法:'post',params:{src:下一个, dir: dir, img: img, ssimg: 下一个} });返回假;

I made a photogallery and is AJAX powered. Below are the relevant pieces of code.

    if (isset($_POST['stopss'])){
    echo'<script type="text/javascript">
    clearTimeout(setss);
    </script>';
    }

    if (isset($_POST['startss'])){
    ?>
    <script type="text/javascript">
    var setss = window.setTimeout('delayer(\''+src+'\',\''+dir+'\',\''+title+'\',\''+width+'\',\''+height+'\',\''+img+'\')',5000);
    </script>
    <?
    }

The delayer function passes params for a new modal box window (new AJAX request).
The problem as I see it. The timeout gets started when a user clicks the Slideshow button.
Even though they press the Stop Slideshow button (this reloads the AJAX page), the timer is still running (in the background) and in clearTimeout(setss), setss is no longer available (because of the new AJAX page request) so the clearTimeout fails to clear.

Is there any way to kill the timeout running in the background? Even if the modalbox window is closed, 5 seconds later it opens and happily keeps playing the slide show.

btw, this is the modalbox call.
Modalbox.show('../b-gal-scripts/ajaxgal.php', {title: title, overlayOpacity: .1, width: width, height: height, method: 'post', params: {src: next, dir: dir, img: img, ssimg: next} }); return false;

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

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

发布评论

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

评论(2

纵性 2024-12-27 20:04:35

问题

当您重新加载页面时,旧的 JavaScript 代码(用于上一个请求)将停止运行并执行新的代码。

问题是您首先调用clearTimeout(setss),然后定义setss到底是什么。

几个额外的问题

一些提示:

  1. 不要混合引用样式,这样会造成混乱。
  2. 如果您想在引号中引用引号,只需使用存在不同引号的事实,如下所示:

    var sets = window.setTimeout("delayer('"+src+'","'+dir+'","'+title+'","'+width+'","'+height+'" ,"'+img+'")',5000);
    
  3. ...并且最好习惯基于事件JavaScript 编码时的编程闭包,如下所示:

    var set = window.setTimeout(function(){
        延迟器(src,目录,标题,宽度,高度,img);
    }, 5000);
    

代码的最终版本 你

的代码应该如下所示:

if (isset($_POST['startss']) && !isset($_POST['stopss'])){
    ?>
    <script type="text/javascript">
        var setss = window.setTimeout(function(){
            delayer(src, dir, title, width, height, img);
        }, 5000);
    </script>
    <?
}

它利用了这样一个事实,即您不需要首先输出一个脚本,执行该脚本只是为了在不需要时立即停止它。只需设置 PHP 条件来检查是否要执行它,然后输出它。

其次,JS 已从使用字符串(完全混乱)改为使用匿名函数。仔细看看。

有帮助吗?

The problem

When you reload the page, the old JavaScript code (for the previous request) stops running and the new one is executed.

The problem is that you first call clearTimeout(setss) and later define what setss really is.

A couple additional problems

A few tips:

  1. Do not mix quoting styles, you are creating a mess this way.
  2. If you want quotes within quotes, just use the fact there are different quotes like that:

    var setss = window.setTimeout("delayer('"+src+'","'+dir+'","'+title+'","'+width+'","'+height+'","'+img+'")',5000);
    
  3. ...and preferably get used to event-based programming and closures when coding JavaScript, like that:

    var sets = window.setTimeout(function(){
        delayer(src, dir, title, width, height, img);
    }, 5000);
    

Final version of your code

Your code should look like the following:

if (isset($_POST['startss']) && !isset($_POST['stopss'])){
    ?>
    <script type="text/javascript">
        var setss = window.setTimeout(function(){
            delayer(src, dir, title, width, height, img);
        }, 5000);
    </script>
    <?
}

It makes use of the fact, that you do not need to first output a script that is executed only to immediately stop it if not needed. Just make PHP condition to check if you want it to be executed, the output it.

Second, the JS has been changed from using strings (which is totally messy) to using anonymous functions. Take a closer look into that.

Did it help?

莳間冲淡了誓言ζ 2024-12-27 20:04:35

我有点难以理解你的问题,但我希望这会有所帮助。

  1. 重新加载页面时,该页面的前一个实例以及与其关联的任何状态都将不再存在。结果,超时被停止了。本质上,如果全局 setSS 不存在,那么它所引用的超时也不存在,因此,该超时不能负责启动幻灯片放映。

  2. 当活动页面由 PHP 之类的东西提供时,服务器的工作(WRT 创建页面的任何内容)就完成了。随后无法将更多内容写入该页面。您的clearTimeout本质上是无用的,因为它只能清除在页面加载期间设置的超时。它不会将新的脚本元素写入现有页面。

  3. 变量不会跨页面共享,因此如果您在一个页面中设置超时,并在另一个页面中清除它,这是行不通的。

因此,为了清除超时,您需要在与 setTimeout 相同的页面(以及该页面的实例)中调用 clearTimeout,并且该调用必须在setTimeout

如果上述情况都不适合您的情况,那么是否可以在同一个请求中设置 $_POST['startss']$_POST['stopss'] ?如果是这样,新页面将在尝试清除超时后创建新的超时,并因此显示幻灯片。

(顺便说一句,使用闭包创建一个传递给 setTimeout 的函数比编译调用函数的字符串更具可读性)。

I'm having a little difficulty understanding your problem, but I hope this helps.

  1. When a page is reloaded, the previous instance of the page, and any state associated with it ceases to exist. As a result, the timeout has been stopped. Essentially, if the global setSS doesn't exist, then neither does the timeout to which it refers, therefore, that timeout cannot be responsible for starting the slideshow.

  2. When an active page is served from something like PHP, the work of the server (WRT the creation of any content of the page) is done. No more content can subsequently be written to the page. Your clearTimeout is essentially useless, as it could only clear a timeout that is set during the load of the page. It won't write a new script element into the existing page.

  3. Variables are not shared across pages, so if you are setting the timeout in one page, and clearing it in another, that will not work.

So, in order to clear the timeout, you need to call clearTimeout in the same page (and instance of that page) as the setTimeout, and that call must be executed after the setTimeout.

If none of the above apply to your situation, then is it possible that $_POST['startss'] and $_POST['stopss'] are set in the same request? If so, the new page will create a new timeout after the attempt to clear it, and will therefore display the slideshow.

(as an aside, using a closure to create a function to pass to setTimeout will be more readable than compiling a string that calls a function).

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