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

javascript - Is there anyway on onmouseover to fire after mouseovering after a few seconds? - Stack Overflow

programmeradmin2浏览0评论

I want to make sure the user wants to have something pop up by mouseing over a div. I don't want the user to accidentally trigger something to pop up without intent. setTimeout doesn't work because even if it's very brief, the pop up will still pop up, it's just delayed. What I want is the user to mouseover something for 1sec for a pop up to display.

**update:

When I do this:

<div onmouseover="myTimer=setTimeout('display(this)', 5000);">

the timer works and it is displayed after 5 seconds but this is not passed and I can't get the element via $(element).next(), etc. When I do this:

 <div onmouseover="myTimer=setTimeout(display(this), 5000);">

the timer doesn't work. What is wrong, how can I get the timer and the this to be passed?

Thanks!

**update2: the this problem from here states: "Code executed by setTimeout() is run in a separate execution context to the function from which it was called. As a consequence, the this keyword for the called function will be set to the window (or global) object, it will not be the same as the this value for the function that called setTimeout."

I found the answer to overe this here where you have to "save a reference to the context where the setTimeout function call is made"

I want to make sure the user wants to have something pop up by mouseing over a div. I don't want the user to accidentally trigger something to pop up without intent. setTimeout doesn't work because even if it's very brief, the pop up will still pop up, it's just delayed. What I want is the user to mouseover something for 1sec for a pop up to display.

**update:

When I do this:

<div onmouseover="myTimer=setTimeout('display(this)', 5000);">

the timer works and it is displayed after 5 seconds but this is not passed and I can't get the element via $(element).next(), etc. When I do this:

 <div onmouseover="myTimer=setTimeout(display(this), 5000);">

the timer doesn't work. What is wrong, how can I get the timer and the this to be passed?

Thanks!

**update2: the this problem from here states: "Code executed by setTimeout() is run in a separate execution context to the function from which it was called. As a consequence, the this keyword for the called function will be set to the window (or global) object, it will not be the same as the this value for the function that called setTimeout."

I found the answer to overe this here where you have to "save a reference to the context where the setTimeout function call is made"

Share Improve this question edited May 23, 2017 at 10:24 CommunityBot 11 silver badge asked Jan 10, 2012 at 15:14 JCarterJCarter 4073 silver badges11 bronze badges 5
  • 4 You should use setTimeout, but you should then clear it via clearTimeout if you no longer wish to handle the event. – Matt Commented Jan 10, 2012 at 15:16
  • Great idea! I hate it when I roll the mouse around a page and all kinds of unwanted tooltips pop up and obscure the content, and I don't want this to happen to my users. – DOK Commented Jan 10, 2012 at 15:31
  • Try to add quotas into display parameters – DonCallisto Commented Jan 10, 2012 at 16:31
  • @DonCallisto like this: <div onmouseover="myTimer=setTimeout(display('this'), 5000);"> ? It still doesn't work, it gets into the function but this isn't passed the way I want it to. – JCarter Commented Jan 10, 2012 at 16:37
  • @JCarter I updated my answer to call display properly with this. – Paul Commented Jan 11, 2012 at 4:15
Add a ment  | 

4 Answers 4

Reset to default 7

This should work for you:

<div id="example">Hover me</div>
<script type="text/javascript">
    (function(){
        var timer;
        var el = document.getElementById('example');

        el.onmouseover = function(){
            timer = setTimeout(function(){
                display(el);
                // If display takes no arguments and uses the "this" keyword inside it
                // Use display.call(el); instead
            }, 1000);
        }

        el.onmouseout = function(){
            clearTimeout(timer);
        }
    })();
</script>

Example on JSFiddle

I'm pretty sure that this should works:

onmouseover="myTimer=setTimeout(yourFunctionName, 1000);"

You have also to add:

onmouseout="clearTimeout(myTimer);"

On mouse over you can start a timer. On mouse out reset the timer to 0. If the timer reaches 1s, you can trigger your event and reset the timer to 0.

I also later came across something called the hoverIntent jQuery Plug-in, worth checking out.

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>