Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用setTimeout/Cleartimeout时意外的令牌非法_Javascript_Jquery - Fatal编程技术网

Javascript 使用setTimeout/Cleartimeout时意外的令牌非法

Javascript 使用setTimeout/Cleartimeout时意外的令牌非法,javascript,jquery,Javascript,Jquery,这就是我想要的:当我悬停在div外时超时,如果我再次悬停在div顶部时取消 如果我删除cancel函数,代码就会工作。所以显示和隐藏div效果很好 这是我的代码: $(document).ready(function () { var timeoutID; function clearAlert() { window.clearTimeout(timeoutID); }​ if ($

这就是我想要的:当我悬停在div外时超时,如果我再次悬停在div顶部时取消

如果我删除cancel函数,代码就会工作。所以显示和隐藏div效果很好

这是我的代码:

$(document).ready(function ()
    {
        var timeoutID;
        function clearAlert()
        {
            window.clearTimeout(timeoutID);
        }​

        if ($('.txtVote').text() == "Avgi stemme")
        {
            $('#starInfo').hover(
            function ()
            {
                clearAlert();
                $('#CarDetailsButtons').css('right', '10px');
                $('#voteStars').show();
            },
            function ()
            {
                console.log("hide iniated");
                timeoutID = window.setTimeout(hideStars, 2000);
                console.log("hide executed");
            });     
        }

        function hideStars()
        {
            console.log("hideStars ran");
            $('#CarDetailsButtons').css('right', '45px');
            $('#voteStars').hide();
        }
});
这是我的错误:

未捕获的语法错误:意外标记非法

它出现在
窗口后面的行上。clearTimeout(timeoutID)

有人能看出我做错了什么吗?:)

编辑:*有效*

看来我有点复制粘贴错误。我手工编写了clearAlert函数,就像以前一样,现在可以工作了


感谢所有的投入

我建议您将函数移到doc ready之外,如下所示:

function hideStars()
    {
        console.log("hideStars ran");
        $('#CarDetailsButtons').css('right', '45px');
        $('#voteStars').hide();
    }

function clearAlert()
    {
        window.clearTimeout(timeoutID);
    }​

$(document).ready(function ()
  {
    var timeoutID;


    if ($('.txtVote').text() == "Avgi stemme")
    {
        $('#starInfo').hover(
        function ()
        {
            clearAlert();
            $('#CarDetailsButtons').css('right', '10px');
            $('#voteStars').show();
        },
        function ()
        {
            console.log("hide iniated");
            timeoutID = window.setTimeout(hideStars, 2000);
            console.log("hide executed");
        });     
    }


});
试试这个:

var that = this;
that.timeoutID = null;

...

that.timeoutID = ...

您必须声明var timeoutID=null;全球的。因此,var位于gloabl上下文中,而不在$(document).ready的函数上下文中

最好的解决方案是编写一个带有计时器的小js类。因此,您没有全局冲突,并且它是有效的


你好

var-timeouteId
timeoutID
-更改其中一个不起作用:(但我更新了代码。仍然是相同的错误。我怀疑我甚至需要var timeoutID;将var移动到全局范围:
window.timeoutID=“”;
添加了window.timeoutID=“”;但仍然不起作用尝试此操作。