473,378 Members | 1,422 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Newbie needs help with setInterval

Hi Folks,

I'm new to Javascript and just need a little help. I downloaded a
script from Dynamic Drive's Web site and I'm trying to make a simple
modification and could use some help. :)

The script below fetches data from the web server and displays it in
the form as a ticker on the web page. See: http://www.dynamicdrive.com/dynamici...ajaxticker.htm

What I would like to do is to have the Ajax ticker periodically go out
and refetch the XML data say once every five seconds. The way it
operates now it only fetches the data when the page loads. Is there a
simple way to accomplish this? I've tried using
setInterval(this.getXMLfile(), 5000) on line 46 with no success, but
it's most likely the way I'm using it. See script script below.

Any help would be greatly appreciated. :)

Thanks,
Shawn
// -------------------------------------------------------------------
// Ajax XML Ticker (txt file source)
// Author: Dynamic Drive (http://www.dynamicdrive.com)
// -------------------------------------------------------------------

////////////No need to edit beyond here//////////////

function createAjaxObj(){
var httprequest=false
if (window.XMLHttpRequest){ // if Mozilla, Safari etc
httprequest=new XMLHttpRequest()
if (httprequest.overrideMimeType)
httprequest.overrideMimeType('text/xml')
}
else if (window.ActiveXObject){ // if IE
try {
httprequest=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e){
try{
httprequest=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){}
}
}
return httprequest
}

// -------------------------------------------------------------------
// Main Ajax Ticker Object function
// ajax_ticker(xmlfile, divId, divClass, delay, optionalfadeornot)
// -------------------------------------------------------------------

function ajax_ticker(xmlfile, divId, divClass, delay, fadeornot){
this.xmlfile=xmlfile //Variable pointing to the local ticker xml file
(txt)
this.tickerid=divId //ID of ticker div to display information
this.delay=delay //Delay between msg change, in miliseconds.
this.mouseoverBol=0 //Boolean to indicate whether mouse is currently
over ticker (and pause it if it is)
this.pointer=0
this.opacitystring=(typeof fadeornot!="undefined")? "width: 100%;
filter:progid:DXImageTransform.Microsoft.alpha(opa city=100); -moz-
opacity: 1" : ""
if (this.opacitystring!="") this.delay+=500 //add 1/2 sec to account
for fade effect, if enabled
this.opacitysetting=0.2 //Opacity value when reset. Internal use.
this.messages=[] //Arrays to hold each message of ticker
this.ajaxobj=createAjaxObj()
document.write('<div id="'+divId+'" class="'+divClass+'"><div
style="'+this.opacitystring+'">Initializing ticker...</div></div>')
/////////////line added with no success////
setInterval(this.getXMLfile(), 5000)
}

// -------------------------------------------------------------------
// getXMLfile()- Use Ajax to fetch xml file (txt)
// -------------------------------------------------------------------

ajax_ticker.prototype.getXMLfile=function(){
if (this.ajaxobj){
var instanceOfTicker=this
var url=this.xmlfile+"?bustcache="+new Date().getTime()
this.ajaxobj.onreadystatechange=function()
{instanceOfTicker.initialize()}
this.ajaxobj.open('GET', url, true)
this.ajaxobj.send(null)
}
}

// -------------------------------------------------------------------
// initialize()- Initialize ticker method.
// -Gets contents of xml file and parse it using JavaScript DOM
methods
// -------------------------------------------------------------------

ajax_ticker.prototype.initialize=function(){
if (this.ajaxobj.readyState == 4){ //if request of file completed
if (this.ajaxobj.status==200 ||
window.location.href.indexOf("http")==-1){ //if request was successful
this.contentdiv=document.getElementById(this.ticke rid).firstChild //
div of inner content that holds the messages
var xmldata=this.ajaxobj.responseText
this.contentdiv.style.display="none"
this.contentdiv.innerHTML=xmldata
if (this.contentdiv.getElementsByTagName("div").lengt h==0){ //if no
messages were found
this.contentdiv.innerHTML="<b>Error</bfetching remote ticker file!"
return
}
var instanceOfTicker=this
document.getElementById(this.tickerid).onmouseover =function()
{instanceOfTicker.mouseoverBol=1}
document.getElementById(this.tickerid).onmouseout= function()
{instanceOfTicker.mouseoverBol=0}
if (window.attachEvent) //Clean up loose references in IE
window.attachEvent("onunload", function()
{instanceOfTicker.contentdiv=instanceOfTicker.ajax obj=null})
//Cycle through XML object and store each message inside array
for (var i=0; i<this.contentdiv.getElementsByTagName("div").leng th; i+
+){
if (this.contentdiv.getElementsByTagName("div")
[i].className=="message")
this.messages[this.messages.length]=this.contentdiv.getElementsByTagName("div")
[i].innerHTML
}
this.contentdiv.innerHTML=""
this.contentdiv.style.display="block"
this.rotatemsg()
}
}
}

// -------------------------------------------------------------------
// rotatemsg()- Rotate through ticker messages and displays them
// -------------------------------------------------------------------

ajax_ticker.prototype.rotatemsg=function(){
var instanceOfTicker=this
if (this.mouseoverBol==1) //if mouse is currently over ticker, do
nothing (pause it)
setTimeout(function(){instanceOfTicker.rotatemsg() }, 100)
else{ //else, construct item, show and rotate it!
this.fadetransition("reset") //FADE EFFECT- RESET OPACITY
this.contentdiv.innerHTML=this.messages[this.pointer]
this.fadetimer1=setInterval(function()
{instanceOfTicker.fadetransition('up', 'fadetimer1')}, 100) //FADE
EFFECT- PLAY IT
this.pointer=(this.pointer<this.messages.length-1)? this.pointer+1 : 0
setTimeout(function(){instanceOfTicker.rotatemsg() }, this.delay) //
update container periodically
}
}

// -------------------------------------------------------------------
// fadetransition()- cross browser fade method for IE5.5+ and Mozilla/
Firefox
// -------------------------------------------------------------------

ajax_ticker.prototype.fadetransition=function(fade type, timerid){
var contentdiv=this.contentdiv
if (fadetype=="reset")
this.opacitysetting=0.2
if (contentdiv.filters && contentdiv.filters[0]){
if (typeof contentdiv.filters[0].opacity=="number") //IE6+
contentdiv.filters[0].opacity=this.opacitysetting*100
else //IE 5.5
contentdiv.style.filter="alpha(opacity="+this.opac itysetting*100+")"
}
else if (typeof contentdiv.style.MozOpacity!="undefined" &&
this.opacitystring!=""){
contentdiv.style.MozOpacity=this.opacitysetting
}
else
this.opacitysetting=1
if (fadetype=="up")
this.opacitysetting+=0.1
if (fadetype=="up" && this.opacitysetting>=1)
clearInterval(this[timerid])
}
Sep 21 '08 #1
2 2744
On Sep 21, 4:52*pm, shawnwperk...@gmail.com wrote:
Hi Folks,

I'm new to Javascript and just need a little help. I downloaded a
script from Dynamic Drive's Web site and I'm trying to make a simple
modification and could use some help. :)

The script below fetches data from the web server and displays it in
the form as a ticker on the web page. See:http://www.dynamicdrive.com/dynamici...ajaxticker.htm

What I would like to do is to have the Ajax ticker periodically go out
and refetch the XML data say once every five seconds. The way it
operates now it only fetches the data when the page loads. Is there a
simple way to accomplish this? I've tried using
setInterval(this.getXMLfile(), 5000) on line 46 with no success, but
it's most likely the way I'm using it. See script script below.
Well, without spending a lot of time analyzing this code, one simple
thing to fix is changing "this.getXMLfile()" to "this.getXMLfile".
The former actually calls the function, and the return value from that
function is what setInterval is going to attempt to call, which is
certainly not what you intended. Removing the parens makes this a
function object, not a function call, which has a much better chance
of working.
Sep 22 '08 #2
On Sep 22, 4:52*am, shawnwperk...@gmail.com wrote:
What I would like to do is to have the Ajax ticker periodically go out
and refetch the XML data say once every five seconds. The way it
operates now it only fetches the data when the page loads. Is there a
simple way to accomplish this? I've tried using
setInterval(this.getXMLfile(), 5000) on line 46 with no success, but
it's most likely the way I'm using it. See script script below.

Any help would be greatly appreciated. :)
Maybe what you wanted to do was use the variant of setInterval() which
accepts a Javascript expression in the form of Javascript String i.e.
setInterval("this.getXMLFile()", 5000). But then again, it won't work
in your case since `this' when used in such a context always refers to
the global window object. As already suggested, use the second variant
of setInterval() which accepts a Function object as its first
parameter. See < http://developer.mozilla.org/en/DOM/window.setInterval
>
/sasuke
Sep 22 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
10
by: Richard A. DeVenezia | last post by:
At line this.timerId = setInterval (function(){this.step()}, 100) I get error dialog Error:Object doesn't support this property or method. If I change it to this.timerId = setInterval...
17
by: George Hester | last post by:
Hoe can I use setInterval where its argument is a function which also has an argument? For example I have a function change(Msg) where Msg is dynamically generated and returns nothing at this point...
6
by: marktm | last post by:
Hi- I am trying to use setInterval to call a method within an object and have not had any luck. I get "Object doesn't support this property or method" when I execute the following code. What I...
4
by: barry | last post by:
If The script below is exeuted from the onload="startit()" in the body tag everything works fine but when I remove the onload and try to execute by using the attributes.add from within the Page_Load...
1
by: Terry | last post by:
Hi folks. I tried to use setInterval like this but it did not work setInterval ( removeitem (theitem) { theitem.style.display= "none"; }, 1000 ); the function without the setInterval is
1
by: Jeff | last post by:
I've been trying to pass in an object in a setInterval but it complains about missing ] after elenment list. setInterval("someFunction("+some_object+")",1000); What's the correct syntax to do...
7
by: kaiser0427 | last post by:
Ok, I have cleaned up the code and fixed some errors in it. The problem I am having is with the timer. If you uncomment the alert it pauses after every record. when I replace it with setInterval...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.