Tìm kiếm hỗ trợ

Tránh các lừa đảo về hỗ trợ. Chúng tôi sẽ không bao giờ yêu cầu bạn gọi hoặc nhắn tin đến số điện thoại hoặc chia sẻ thông tin cá nhân. Vui lòng báo cáo hoạt động đáng ngờ bằng cách sử dụng tùy chọn "Báo cáo lạm dụng".

Learn More

Javascript execution bug in setTimeout loop when trying to fade.style.opacity += 0.1.

  • 4 trả lời
  • 2 gặp vấn đề này
  • 27 lượt xem
  • Trả lời mới nhất được viết bởi Haapavuo

more options

Javascript execution bug in setTimeout loop when trying to fade.style.opacity += 0.1. It works with negative operator -= but not with a positive operator +=. Browser (Firefox 11) just keeps telling me that the opacity is 0.1 all the time.

Here is my code:

function setOpacity() {
	i++;
	if (dir == 1)
	{
		fade.style.opacity -= 0.1;
		if (fade.style.opacity < 0.1)
		{	dir = 0;	}
	}
	else
	{
		fade.style.opacity = fade.style.opacity + 0.1;
		if (fade.style.opacity > 0.9)
		{	dir = 1;	}
	}
	document.getElementById('opacity').innerHTML = "Opacity: " + fade.style.opacity;
	document.getElementById('dirr').innerHTML = "dir = " + dir;
	document.getElementById('i').innerHTML = "i = " + i;
	t = setTimeout("setOpacity()",100);
}

So if I change the += operator to -=, the opacity keeps changing. I'm pretty sure that the problem must be in Firefox because with IE everything kept going even with the += operator.

Javascript execution bug in setTimeout loop when trying to fade.style.opacity += 0.1. It works with negative operator -= but not with a positive operator +=. Browser (Firefox 11) just keeps telling me that the opacity is 0.1 all the time. Here is my code: <pre><nowiki>function setOpacity() { i++; if (dir == 1) { fade.style.opacity -= 0.1; if (fade.style.opacity < 0.1) { dir = 0; } } else { fade.style.opacity = fade.style.opacity + 0.1; if (fade.style.opacity > 0.9) { dir = 1; } } document.getElementById('opacity').innerHTML = "Opacity: " + fade.style.opacity; document.getElementById('dirr').innerHTML = "dir = " + dir; document.getElementById('i').innerHTML = "i = " + i; t = setTimeout("setOpacity()",100); }</nowiki></pre> So if I change the += operator to -=, the opacity keeps changing. I'm pretty sure that the problem must be in Firefox because '''with IE everything kept going even with the += operator'''.

Được chỉnh sửa bởi Haapavuo vào

Giải pháp được chọn

The problem seems to be that the opacity value isn't seen as a float.

Use these instead:

fade.style.opacity = fade.style.opacity*1 - 0.1;
fade.style.opacity = fade.style.opacity*1 + 0.1;

You may consider using document.getElementById('fade') instead of the fade identifier to avoid warnings in the Error Console.

Warning: Element referenced by ID/NAME in the global scope. Use W3C standard document.getElementById() instead.
Source File: http://greenpark.fi/js_fader.php
Line: 23

Đọc câu trả lời này trong ngữ cảnh 👍 0

Tất cả các câu trả lời (4)

more options

A good place to ask advice about web development is at the MozillaZine "Web Development/Standards Evangelism" forum.

The helpers at that forum are more knowledgeable about web development issues.
You need to register at the MozillaZine forum site in order to post at that forum.


Do you have a page with full testing code?

more options

Yes the code is here: Javascript fader

I still believe that this problem is caused by Firefox, not incorrect code.

dir = 1 --> fade out, dir = 0 --> fade in

i = loop counter

As you can see, the loop keeps going but browser stops changing the opacity. In IE the variables change just fine. On Firefox the opacity stops changing when the text should fade back in.

EDIT: There seems to be a bug in Firefox. It doesn't recognize the '+=' operator as it should. To Firefox 11, '+=' operator seems to be the same as '=' operator.

Được chỉnh sửa bởi Haapavuo vào

more options

Giải pháp được chọn

The problem seems to be that the opacity value isn't seen as a float.

Use these instead:

fade.style.opacity = fade.style.opacity*1 - 0.1;
fade.style.opacity = fade.style.opacity*1 + 0.1;

You may consider using document.getElementById('fade') instead of the fade identifier to avoid warnings in the Error Console.

Warning: Element referenced by ID/NAME in the global scope. Use W3C standard document.getElementById() instead.
Source File: http://greenpark.fi/js_fader.php
Line: 23

more options

Thank you very much!

I made the changes you suggested and got the code to work. So document.getElementById('fade').style.opacity*1 is considered as a float but document.getElementById('fade').style.opacity is not? Weird... I didn't know that a integer multiplier changes the variable type.