PDA

View Full Version : setInterval & setTimeout in FF



temp304
08-16-2007, 08:39 PM
I can not use setInterval & setTimeout functions in FF? Can everybody help me?

Thank before

jscheuer1
08-17-2007, 01:02 AM
Please post a link to the page on your site that contains the problematic script so we can check it out.

temp304
08-17-2007, 03:20 PM
function styleOver(evt)
{
var srcElem = getObject(evt); // getObject return a object;

timer = window.setInterval('setTimer(' + srcElem.id + ')', 1000);
}

above function work in IE and Opera, but FF is not. I don't know why?

jscheuer1
08-17-2007, 03:33 PM
I would suspect that:



var srcElem = getObject(evt); // getObject return a object;

doesn't. But, as I said:

Please post a link to the page on your site that contains the problematic script so we can check it out.

I would at least need to see the setTimer() and getObject() functions to have a clue. There are other minor problems with the code you did present, but these shouldn't cause the problem that you are talking about.

Twey
08-17-2007, 03:39 PM
It would also be better to pass a function to setTimeout():
function styleOver(evt) {
var srcElem = getObject(evt);
timer = setInterval(function() {
setTimer(srcElem.id);
}, 1000);
}If you're passing an ID, it's likely that you actually want to pass an element to setTimer() but were limited by having to pass a string. If that's so, you should adapt setTimer() to take a direct element reference instead: it's more efficient and cuts down on bulk code.

Academically, since you should be using the above code anyway, the major problem with this code:
timer = window.setInterval('setTimer(' + srcElem.id + ')', 1000);was probably that you forgot to add quotes to the string to be executed by setTimeout(). The final string would have been something like this (if srcElement.id === "pd"):
setTimer(pd)In Firefox and other non-idiotic browsers, elements are not automatically added to the global scope under their IDs, so this code would fail. It would probably work if you were to do
timer = window.setInterval('setTimer("' + srcElem.id + '")', 1000);... but as already explained, you shouldn't.

jscheuer1
08-17-2007, 03:52 PM
timer = window.setInterval('setTimer("' + srcElem.id + '")', 1000);

That's a very good point, if that were the only problem, then this would also work:


timer = window.setInterval('setTimer(' + srcElem + ')', 1000);

or, better form:


timer = setInterval(function(){setTimer(srcElem);}, 1000);

But I suspect that there may be other problems.

Twey
08-17-2007, 04:57 PM
That's a very good point, if that were the only problem, then this would also work:
timer = window.setInterval('setTimer(' + srcElem + ')', 1000);No it wouldn't... if srcElem is an object (presumably an element), then that will render into something like:
setInterval('setTimer([object HTMLDivElement])', 1000);... which makes no sense at all (and is still lacking quotes). This is the biggest limitation of passing a string to setTimeout(): only objects that can be accurately represented as strings can be passed.
or, better form:
timer = setInterval(function(){setTimer(srcElem);}, 1000);This, however, would pass the srcElem object to setTimer(), which presumably expects a string ID -- thus, my comment on adapting setTimer().
But I suspect that there may be other problems.It's possible. temp304, if none of these ideas solves your problem, we really do need to see more of your code.

jscheuer1
08-17-2007, 05:15 PM
Yes, it gets confusing. However, if your original idea Twey, which I think is a good possibility in this case and a good point in general, if that's true, then the setTimer() function is actually expecting an object. Or, perhaps an object and a string. In any event, if that's what is happening, setTimer() is poorly written and I'd need to see it to correct it.

I'd really prefer to see all the code, just in case. That way we wouldn't have to keep going back and asking for more. If it's more code than we need, there is no harm done.

I get a little tired of people asking for help and then making it like pulling teeth to get any information about the problem.

Twey
08-17-2007, 06:00 PM
However, if your original idea Twey, which I think is a good possibility in this case and a good point in general, if that's true, then the setTimer() function is actually expecting an object.Huh? How? Neither the OP nor myself ever passed it anything other than a string. I was saying it should be rewritten to take an object, not that it already did.
I get a little tired of people asking for help and then making it like pulling teeth to get any information about the problem.Well, agreed.

jscheuer1
08-17-2007, 06:05 PM
However, if your original idea Twey, which I think is a good possibility in this case and a good point in general, if that's true, then the setTimer() function is actually expecting an object.

Huh? How? Neither the OP nor myself ever passed it anything other than a string.

My thinking was that since the argument was obviously passed without delimiters, it must be being treated in the destination function as an object. Not even IE will treat an argument without delimiters as a string.

Twey
08-17-2007, 06:32 PM
Ah, I see. No, srcElem.id is never passed (and wouldn't be even if it were included in the string literally, since the string is executed at global scope): the value of its value is passed.

temp304
08-18-2007, 09:47 AM
timer = window.setInterval('setTimer("' + srcElem.id + '")', 1000);... but as already explained, you shouldn't.

=> that's all to solve my problem.

I really want to thank everybody who disscussed this topic. Again, thank you very much

Twey
08-18-2007, 02:59 PM
=> that's all to solve my problem.You really should rewrite that function and pass the whole srcElem object via a closure.