Jump to content

Add delay in JS


cclloyd9785

Recommended Posts

I was looking at the script for Snap Links lite for Chrome, and after looking on this site at the tutorials and such, there are a couple things I need help with.1. I don't see exactly where in the script is where it actually opens the links2. Because of number 1, I couldnt find out where to add a delay, plus I couldn't find out how to here either.Posted is the code for it. Sorry its sorta long, but it might also benefit me to tell you that there was an options page, with the options named 'reverse', 'visibleonly' and I added one called 'delaylink'.

var ox = 0;var oy = 0;var onDown = false;var onWork = false;var obj = false;var objSub = [];var objSubLink = [];var blockContextMenu = false;var visibleOnly = 1;var button = 2;var reverse = 0;chrome.extension.sendRequest({message:'snaplinksInit'}, function(ret){	visibleOnly = ret.visibleOnly;	button = ret.button;	reverse = ret.reverse;});function eventMouseDown(e){	if(e.button != button)		return true;	e.stopPropagation();	e.preventDefault();	onDown = true;	blockContextMenu = false;	ox = e.pageX;	oy = e.pageY;}function getBoxObjectForXY(element){	var x = 0;	var y = 0;	var parent = element;	while ( parent ) {		x += parent.offsetLeft;		y += parent.offsetTop;		parent = parent.offsetParent;	}	parent = element;	while ( parent &&	parent != document.body &&	parent != document.documentElement ) {		if ( parent.scrollLeft  )			x -= parent.scrollLeft;		if ( parent.scrollTop )			y -= parent.scrollTop;		parent = parent.parentNode;	}	return { x:x, y:y};}function isVisible(obj){	var parent = obj;	while ( parent ) {		if(window.getComputedStyle(parent, null).display == 'none')			return false;		if(window.getComputedStyle(parent, null).visibility == 'hidden')			return false;		if(parseInt(window.getComputedStyle(parent, null).zIndex) < 0)			return false;		parent = parent.offsetParent;	}	return true;}function getBoxObjectFor(obj){	var xy = getBoxObjectForXY(obj);	return {		x:xy.x		,y:xy.y		,width:obj.offsetWidth		,height:obj.offsetHeight	};}function eventMouseMove(e){	if(!onDown)		return true;	e.stopPropagation();	e.preventDefault();	if(!onWork)	{		if(Math.abs(ox - e.clientX) > 3			&& Math.abs(oy - e.clientY) > 3)		{			onWork = true;			obj = document.createElement("div");			(document.body || document.documentElement).appendChild(obj);			obj.style.position = "absolute";			obj.style.left = ox + "px";			obj.style.top = oy + "px";			obj.style.border = "1px dotted green";		}	}	if(onWork)	{		if(e.pageX > ox)			obj.style.width = (e.pageX - ox) + "px";		else		{			obj.style.left = e.pageX + "px";			obj.style.width = (ox - e.pageX) + "px";		}		if(e.pageY > oy)			obj.style.height = (e.pageY - oy) + "px";		else		{			obj.style.top = e.pageY + "px";			obj.style.height = (oy - e.pageY) + "px";		}		while(objSub.length)			obj.removeChild(objSub.pop());		objSubLink = [];		var tsize = getBoxObjectFor(obj);		for(i = 0; i < document.links.length; i++){			var link = document.links[i];			lsize = getBoxObjectFor(link);			if(link.href && link.href.indexOf('java script:') >= 0)				continue;			if(!lsize || lsize.y + lsize.height < tsize.y || tsize.y + tsize.height < lsize.y)				continue;			if(!lsize || lsize.x + lsize.width < tsize.x || tsize.x + tsize.width < lsize.x)				continue;			if(visibleOnly && !isVisible(link))				continue;			var lover = document.createElement("div");			lover.style.position = "absolute";			lover.style.zIndex = "65535";			lover.style.left = (lsize.x - tsize.x - 2) + "px";			lover.style.top = (lsize.y - tsize.y - 2) + "px";			lover.style.width = lsize.width + "px";			lover.style.height = lsize.height + "px";			lover.style.border = "1px solid red";			objSub.push(lover);			objSubLink.push(link.href);			obj.appendChild(lover);		}	}}function eventMouseUp(e){	if(e.button != button)		return true;	if(onWork)	{		e.stopPropagation();		e.preventDefault();		for(var k in objSubLink)			for(var j in objSubLink)				if(k != j && objSubLink[k] == objSubLink[j])					objSubLink[j] = false;		if(reverse)			objSubLink.reverse();		for(var k in objSubLink)			if(objSubLink[k])				chrome.extension.sendRequest({message:'snaplinks',url:objSubLink[k]});		blockContextMenu = true;		obj.parentNode.removeChild(obj);		objSub = [];		objSubLink = [];	}	obj = onDown = onWork = false;}function eventContextmenu(e){	if(button == 2)		if(blockContextMenu)			e.preventDefault();}function eventMouseDrag(e){	if(e.button != button)		return true;}window.addEventListener("mousedown", eventMouseDown, true);window.addEventListener("mousemove", eventMouseMove, true);window.addEventListener("mouseup", eventMouseUp, true);window.addEventListener("mousedrag", eventMouseDrag, true);document.addEventListener("contextmenu", eventContextmenu, true);

Link to comment
Share on other sites

See, but from what else I see thats only if the reverse option is set to Yet, and not many people, including myself, have that set to yes.
No, the for loop is not part of the if block. The only line in the if block is the call to the reverse method. It always runs the loop.
And in any other section That line isnt there.
So, are you saying I'm wrong? Which other line in that code looks like it's opening a link or sending a request to a URL? There's no point in guessing if that's the right line when it's so easy to test:
			if(objSubLink[k])				{				  alert('opening link');				  chrome.extension.sendRequest({message:'snaplinks',url:objSubLink[k]});				  alert('link opened');				}

You'll see one alert box, then whatever you expect to see when the link opens, then a second alert box. The only thing you'll see between the two alerts is whatever is supposed to happen when the request gets sent. If the request causes the page to reload or change then you might not see the second alert.

It also doesnt answer how to add delay.
If by "add delay" you mean make the effect happen after some time, the techniques to make that happen are the same regardless of what you're trying to delay. You use setTimeout if you want to schedule something to happen once, and you use setInterval if you want to schedule it to happen more than once.http://www.w3schools.com/js/js_timing.asp
Link to comment
Share on other sites

##CUT##If by "add delay" you mean make the effect happen after some time, the techniques to make that happen are the same regardless of what you're trying to delay. You use setTimeout if you want to schedule something to happen once, and you use setInterval if you want to schedule it to happen more than once.http://www.w3schools.com/js/js_timing.asp
ohh maan !!!i know that page, but setInterval() is not on there !i have just found it here;http://www.w3schools.com/jsref/met_win_setinterval.aspi tried that 'clock' code with the setTimeout() function, and thought that was it - you loop a setTimeout() function !!NOW i find out, one just uses setInterval() and JS parses the loop FOR you !! :)
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...