最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

settimeout - How to make delay in calling a function in javascript? - Stack Overflow

programmeradmin4浏览0评论

I want to execute a particular number of statements after some delay. For eg:

function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent) {
        while (1) {
            curleft += obj.offsetLeft;
            if (!obj.offsetParent) {
                break;
            }
            obj = obj.offsetParent;
        }
    } else if (obj.x) {
        curleft += obj.x;
    }
    return curleft;
}

function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
        while (1) {
            curtop += obj.offsetTop;
            if (!obj.offsetParent) {
                break;
            }
            obj = obj.offsetParent;
        }
    } else if (obj.y) {
        curtop += obj.y;
    }
    return curtop;
}
function gotoDiv(index) {
    var ele = document.getElementById("Div" + index);
    var x = findPosX(ele);
    var y = findPosY(ele);
    setTimeout("window.scrollTo(x, y)", 5000);
}

Here I want to set the current scroll position to a prticular div. But it's giving me error: x is undefined. Let me tell u if I use the functions as below it works fine, so please don't tell me that ele is null and blah blah blah.

function gotoDiv(index) {
    var ele = document.getElementById("Div" + index);
    var x = findPosX(ele);
    var y = findPosY(ele);
    window.scrollTo(x, y);
}

Any help appreciated.

I want to execute a particular number of statements after some delay. For eg:

function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent) {
        while (1) {
            curleft += obj.offsetLeft;
            if (!obj.offsetParent) {
                break;
            }
            obj = obj.offsetParent;
        }
    } else if (obj.x) {
        curleft += obj.x;
    }
    return curleft;
}

function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
        while (1) {
            curtop += obj.offsetTop;
            if (!obj.offsetParent) {
                break;
            }
            obj = obj.offsetParent;
        }
    } else if (obj.y) {
        curtop += obj.y;
    }
    return curtop;
}
function gotoDiv(index) {
    var ele = document.getElementById("Div" + index);
    var x = findPosX(ele);
    var y = findPosY(ele);
    setTimeout("window.scrollTo(x, y)", 5000);
}

Here I want to set the current scroll position to a prticular div. But it's giving me error: x is undefined. Let me tell u if I use the functions as below it works fine, so please don't tell me that ele is null and blah blah blah.

function gotoDiv(index) {
    var ele = document.getElementById("Div" + index);
    var x = findPosX(ele);
    var y = findPosY(ele);
    window.scrollTo(x, y);
}

Any help appreciated.

Share Improve this question edited Oct 9, 2009 at 21:47 bdukes 156k25 gold badges150 silver badges176 bronze badges asked Sep 25, 2009 at 12:49 ManishManish 6,28619 gold badges66 silver badges91 bronze badges 1
  • ele being null probably won't cause the x being undefined, I think natrium has that solution, but you should check ele for null though ;) i'm sure you've just simplified the code here and you're actually doing it in your code already ;) – dharga Commented Sep 25, 2009 at 12:57
Add a ment  | 

3 Answers 3

Reset to default 14

You can give setTimeout a function, rather than a string, which will let you access those variables:

setTimeout(function() { window.scrollTo(x, y); }, 5000);      

try

setTimeout("window.scrollTo(" + x + ", " + y + ")", 5000);

this is not best practice. Use this in stead:

setTimeout(function() { window.scrollTo(x, y); }, 5000);

Wrap the function in a closure and then put the timeout inside the closure.

发布评论

评论列表(0)

  1. 暂无评论