简体   繁体   English

Javascript setTimeout-recursion不适用于Chrome和Firefox

[英]Javascript setTimeout-recursion doesn't work with Chrome and Firefox

The code below is for sliding a div inside another hidden overflow div (concept for future website). 下面的代码用于在另一个隐藏的溢出div(用于将来的网站的概念)内滑动div。 The text box below the "next" and "previous" links is for indicating the x value. “下一个”和“上一个”链接下方的文本框用于指示x值。 With IE8 everything works just fine. 使用IE8,一切正常。

With Chrome and Firefox it seems to do the function only once (one step) both "next" and "prev", which means setTimeout doesn't run. 对于Chrome和Firefox,似乎“下一个”和“上一个”仅执行一次功能(一步),这意味着setTimeout无法运行。

<html>
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <head>
        <script type="text/javascript">

            var ie5 = (document.all && document.getElementById);
            var ns6 = (!document.all && document.getElementById);

            function next(startx, fadeStep, end){
                if(startx > end){
                    startx -= Math.round((1/Math.pow(fadeStep,0.6)));
                    if (startx < end) startx = end;
                    document.getElementById('lastname').value = startx + "px";
                    document.getElementById('test1').style.left = startx + "px";
                    fadeStep += 0.00035;
                    args = "next(" + startx + "," + fadeStep + "," + end + ")";
                    setTimeout(args, 20); 
                }
            }

            function prev(startx, fadeStep, end){
                if(startx < end){
                    startx += Math.round((1/Math.pow(fadeStep,0.6)));
                    if (startx > end) startx = end;
                    document.getElementById('lastname').value = startx + "px";
                    document.getElementById('test1').style.left = startx + "px";

                    setTimeout('prev(' + startx + ',' + (fadeStep+0.00035) + ',' + end + ')', 20);
                }
            }

        </script>
    </head>
    <body>
        <div style="position:relative;width:570;height:78;overflow:hidden">
            <div id="test1" style="position:absolute;left:0px;top:0px;width:1180;height:78">
                <img src="http://img38.imageshack.us/img38/7225/imageva.png" border=1>
                <img src="http://img577.imageshack.us/img577/5554/image2te.png" border=1>
            </div>
        </div>
        <div style="position:absolute;left:900px;top:0px;width:800">
            <a href="#" onclick="next(0, 0.001, -570)">Next</a><br />
            <a href="#" onclick="prev(-570, 0.001, 0)">Previous</a></ br>
            <form><input type="text" id="lastname" value="gfgfg"/></form>
        </div>

    </body>
</html>

Can you please assist? 你能帮忙吗?

It might be a better way, in this case, to use setTimeout like the following: 在这种情况下,使用setTimeout可能是一种更好的方法,如下所示:

window.setTimeout(function() { next(startx, fadeStep, end); } , delayInMiliseconds);

However, the lack of the "window." 但是,缺少“窗口”。 object prefix might be another cause of your functionality not working in these browsers. 对象前缀可能是导致您的功能无法在这些浏览器中运行的另一个原因。

If you have any other questions don't hesitate to ask. 如果您还有其他问题,请随时提出。

As stated in the comments it would save a good deal of time to use jQuery. 如评论中所述,使用jQuery将节省大量时间。

Best regards, 最好的祝福,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 javascript setTimeout(); 在linux上不起作用(firefox) - javascript setTimeout(); doesn't work on linux (firefox) JavaScript中的setTimeout不起作用 - setTimeout in JavaScript doesn't work 为什么基于setTimeout的代码不能在Firefox中以很小的超时时间运行(在Internet Explorer / Chrome中运行)? - Why doesn't this setTimeout-based code work in Firefox with a small timeout (works in Internet Explorer/Chrome)? setTimeout 在 Chrome 中似乎不起作用 - setTimeout doesn't seem to work in Chrome Javascript不适用于Chrome和Firefox,但适用于IE - Javascript doesn't work on Chrome and Firefox, but works on IE JavaScript 在 Safari 中不起作用,但在 Firefox 和 Chrome 中起作用 - JavaScript doesn't work in Safari but does in Firefox and Chrome JavaScript时钟可在Chrome上使用,但不适用于Firefox或IE - JavaScript clock works on Chrome but doesn't work with Firefox or IE JavaScript代码在chrome上有效,在IE和Firefox上无效 - JavaScript code works on chrome, doesn't work on IE and firefox Javascript 自动完成功能适用于 IE 和 Chrome,不适用于 Firefox - Javascript autocomplete works in IE and Chrome, doesn't work in Firefox 为什么JavaScript不适用于Firefox / Chrome但适用于IE? - Why doesn't the JavaScript work for Firefox/Chrome but for IE?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM