JavaScript setTimeout this对象指向问题

上面这幅图片是原始的效果,
现在想鼠标移到图标上,显示图标的提示,但需要延时,也就是鼠标滑至图标上,并不立刻显示,而是等1秒后显示。

html部分
<div class="porHeadRit" >
<a href="" class="a srch s-alt"><span>搜索</span></a>
<a href="" class="a msg m-alt"><span>用户消息</span></a>
<a href="" class="a usr u-alt"><span>个人信息</span></a>
</div>
css样式:
.porHeadRit a.s-alt span,.porHeadRit a.m-alt span,.porHeadRit a.u-alt span{display:none;position:absolute;top:62px;width:56px;
height:24px;line-height:26px;z-index:1;border-radius: 2px;left:0px; background-color: rgba(38, 39, 40, 0.8);font-size: 10px;
font-weight: 400;text-align:center;color:#fff;background:url(../images/info-tip.png) no-repeat;}
通过定位来实现图片的显示在图标的正下方

实现鼠标滑至图标上,延时一秒钟显示,只能通过js来实现
开始写的代码很low,但可以实现功能,就是代码非常臃肿,大家可以看下,后来觉得这也太小儿科了
var timer = null;
function sshowCon(){
clearTimeout(timer);
timer = setTimeout(function(){
$(".s-alt span").show();
},1000);
};
function shideCon(){
clearTimeout(timer);
timer = setTimeout(function(){
$(".s-alt span").hide();
},300);
};
function mshowCon(){
clearTimeout(timer);
timer = setTimeout(function(){
$(".m-alt span").show();
},1000);
};
function mhideCon(){
clearTimeout(timer);
timer = setTimeout(function(){
$(".m-alt span").hide();
},300);
};
function ushowCon(){
clearTimeout(timer);
timer = setTimeout(function(){
$(".u-alt span").show();
},1000);
};
function uhideCon(){
clearTimeout(timer);
timer = setTimeout(function(){
$(".u-alt span").hide();
},300);
};
看到了都想笑,后来尝试着优化,写更少的代码,来实现同样的功能,我的代码是这样写的,见下
var spanDom = $(".porHeadRit a");
for(var i=0;i<spanDom.length;i++){
spanDom[i].onmouseover = function(){
clearTimeout(timer);
timer = setTimeout(function(){
$(".porHeadRit a span").show();
},1000);
};
spanDom[i].onmouseleave = function(){
clearTimeout(timer);
timer = setTimeout(function(){
$(".porHeadRit a span").hide();
},300);
};
}
不能实现功能,就是鼠标滑上去,所有的图标的提示都显示了。for循环写的有问题,但也向好的方向的改进。
后来向别人请教,别人提到了jQuery有这样一个方法
$("你的对象").hover(function1,function2);
该函数具体可以参考http://www.jquery123.com/hover/
但是还是没有解决,那是因为在实现的过程setTimeout传this对象是一个很大的坑,开始是这样写的代码
var timer = null;
$(".porHeadRit a").mouseover(function(){
clearTimeout(timer);
timer = setTimeout(function(){
$(this).find('span').show().parent().siblings().children("span").hide();
},500);
}).mouseout(function(){
var that = this;
clearTimeout(timer);
timer = setTimeout(function(){
$(this).find('span').hide();
},300);
});
但是上述写法没有效果,后来查了下,才知道setTimeout传this对象,有个指向的问题,如果没有指定,指向的是全局对象,也就是window.
后来查了下,有一种解决方法,这种解法可以实现鼠标滑至图标处,显示提示,但是没有延时。搞了好久,不知怎么解决。
var timer = null;
$(".porHeadRit a").mouseover(function(){
clearTimeout(timer);
timer = setTimeout(function(self){
$(self).find('span').show().parent().siblings().children("span").hide();
}(this),500);
}).mouseout(function(){
clearTimeout(timer);
timer = setTimeout(function(self){
$(that).find('span').hide();
}(this),300);
});
后来在网上查了解决方法,将this赋给that,通过that来指向当前对象,试了下可以解决问题
var timer = null;
$(".porHeadRit a").mouseover(function(){
var that = this;
clearTimeout(timer);
timer = setTimeout(function(){
$(that).find('span').show().parent().siblings().children("span").hide();
},500);
}).mouseout(function(){
var that = this;
clearTimeout(timer);
timer = setTimeout(function(){
$(that).find('span').hide();
},300);
});
说明:$("").hover(function1,function2);的简写方法是
$("").mouseover().mouseout();用的是jQuery的链式写法。
记录码代码时的问题。
JavaScript setTimeout this对象指向问题的更多相关文章
- JavaScript基础--DOM对象(十三):(windows对象:history\location\navigator\screen\event)
DOM编程1.为什么要学习DOM(1) 通过dom编程,我们可以写出各种网页游戏(2)dom编程也是ajax的重要基础2.DOM编程介绍DOM = Document Object Model(文档对象 ...
- JavaScript内置对象与原生对象【转】
原文:https://segmentfault.com/a/1190000002634958 内置对象与原生对象 内置(Build-in)对象与原生(Naitve)对象的区别在于:前者总是在引擎初始化 ...
- javascript中的对象,原型,原型链和面向对象
一.javascript中的属性.方法 1.首先,关于javascript中的函数/“方法”,说明两点: 1)如果访问的对象属性是一个函数,有些开发者容易认为该函数属于这个对象,因此把“属性访问”叫做 ...
- JavaScript学习04 对象
JavaScript学习04 对象 默认对象 日期对象Date, 格式:日期对象名称=new Date([日期参数]) 日期参数: 1.省略(最常用): 2.英文-数值格式:月 日,公元年 [时:分: ...
- JavaScript内置对象与原型继承
(一) 理解JavaScript类定义 1>关于内置对象理解 console.log(Date.prototype.__proto__===Object.prototype //tru ...
- 全面理解Javascript中Function对象的属性和方法
http://www.cnblogs.com/liontone/p/3970420.html 函数是 JavaScript 中的基本数据类型,在函数这个对象上定义了一些属性和方法,下面我们逐一来介绍这 ...
- javascript第一弹——对象
一. 什么是对象 对象是包含一组变量(称为属性)和函数(称为方法)的集合的实例. javascript中所有事物都是对象 javascript有很多内建对象 javascript允许自定义对象 对象只 ...
- javascript --- jQuery --- Deferred对象
javascript --- jQuery --- Deferred对象 javascript的函数式编程是多么引人入胜,jQuery使代码尽可能的精简,intelligent! defer - 必应 ...
- javascript之window对象
window :window对象是BOM中所有对象的核心,除了是BOM中所有对象的父对象外,还包含一些窗口控制函数. 1.全局的window对象 JavaScript中的任何一个全局函数或变量都是wi ...
随机推荐
- Vue--公有组件以及组件的使用和特点
组件的作用:为了能够让功能与功能之间互不影响,使代码更加清晰整洁 1 <!DOCTYPE html> <html lang="en"> <head&g ...
- golang中包的初始化
1.当一个go源程序被初始化时,首先去初始化所依赖的其他包,然后初始化该go源码文件的全局变量的初始化和执行初始化函数,其中该包所有的全局变量初始化在前,该包的初始化函数int在后.当所有包的初始化函 ...
- OSGi教程:Resource API Specification
此教程基于OSGi Core Release 7 OSGi Resource API规范 详细内容上面英文教程有详细解答 下面主要是一些个人见解,若有不当之处,欢迎指出: Resource:就是能够被 ...
- 网页性能测试之WebPageTest
想知道您的网站,性能怎么样? 很自然,首先得找一个广被认可的测试工具.我们推荐WebPageTest: WebPageTest 它是google 开源项目”make the web faster “的 ...
- LeetCode Top 100 Liked Questions in Golang(updating...)
leetcode go语言版本,主要为了熟悉下语言 1. Two Sum 双指针版本, O(NlogN) func twoSum(nums []int, target int) []int { val ...
- js表格上下移动添加删除
html部分 <div onclick='fn()'>加</div> <table width="250" border="1" ...
- Leetcode821.Shortest Distance to a Character字符的最短距离
给定一个字符串 S 和一个字符 C.返回一个代表字符串 S 中每个字符到字符串 S 中的字符 C 的最短距离的数组. 示例 1: 输入: S = "loveleetcode", C ...
- springboot web开发【转】【补】
pom.xml引入webjars的官网 https://www.webjars.org/ https://www.thymeleaf.org/doc/tutorials/3.0/usingthymel ...
- phpexcel使用说明3
下面是总结的几个使用方法 include 'PHPExcel.php'; include 'PHPExcel/Writer/Excel2007.php'; //或者include 'PHPExcel/ ...
- docker images列出镜像
命令:docker images Usage: docker images [OPTIONS] [REPOSITORY[:TAG]] List images Options: -a, --all Sh ...