Use ajax technology to dynamically call Sina stock real-time data without refreshing, and ajax shares in real time

Source: Internet
Author: User

Use ajax technology to dynamically call Sina stock real-time data without refreshing, and ajax shares in real time

Sina's financial channel has always felt that it is doing well. However, due to the slow speed recently, the webpage cannot be opened when viewing stock information. I have been studying ajax over the past few days, so I used jquery to create a page to automatically read the real-time data of Sina stocks.

<Html> 

Use prototype to replace the script code.
Copy the Code as follows:

<script type="text/javascript" src="prototype.js"></script> <script type="text/javascript">... function ajaxRequest()...{ var myAjax = new Ajax.Request( 'http://hq.sinajs.cn/list=sh000001,sh601939,sh600016,sh600528,sh600667,sh601390,sh601398,sh601857,sh600028', ...{ method: 'get', onComplete: setData } ); } function setData(response) ...{ var contents = response.responseText; var stocks = contents.split(';'); for(var i=0; i<stocks.length-1; i++)...{ var content = stocks[i]; var temp1 = content.split('=')[0]; var temp2 = content.split('=')[1]; var code = temp1.substr(temp1.length - 6, 6); var temp3 = temp2.replace('"', ''); var name = temp3.split(',')[0]; var tday_f = temp3.split(',')[1]; var yest_f = temp3.split(',')[2]; var curr_f = temp3.split(',')[3]; var temp_f = curr_f - yest_f; $('a'+i).innerHTML = code; $('b'+i).innerHTML = name; $('c'+i).innerHTML = curr_f; if(curr_f > yest_f) ...{ $('c'+i).innerHTML = "<font color='red'>" + curr_f + "</font>"; } else if(curr_f < yest_f) ...{ $('c'+i).innerHTML = "<font color='green'>" + curr_f + "</font>"; } else ...{ $('c'+i).innerHTML = curr_f; } $('d'+i).innerHTML = tday_f; $('e'+i).innerHTML = yest_f; if(temp_f > 0) ...{ $('f'+i).innerHTML = "<font color='red'>" + temp_f.toFixed(2) + "</font>"; $('g'+i).innerHTML = "<font color='red'>" + ((temp_f / yest_f) * 100).toFixed(2) + "</font> %"; } else if(temp_f < 0) ...{ $('f'+i).innerHTML = "<font color='green'>" + temp_f.toFixed(2) + "</font>"; $('g'+i).innerHTML = "<font color='green'>" + ((temp_f / yest_f) * 100).toFixed(2) + "</font> %"; } else ...{ $('f'+i).innerHTML = temp_f.toFixed(2); $('g'+i).innerHTML = ((temp_f / yest_f) * 100).toFixed(2) + " % "; } $('h'+i).innerHTML = temp3.split(',')[4]; $('i'+i).innerHTML = temp3.split(',')[5]; } } function pageInit() ...{ window.setInterval("ajaxRequest()",3000); } </script>

Of course, the page can be automatically generated by the script, as well as the dynamic addition and deletion of Stock Information on the page, and so on, just wait for future modification.


Ajax prompt without refreshing in real time

First, this is indeed Ajax technology (as well as Flex technology)

The general idea is to call a javascript function that you have defined when you buy a product. This function passes an information to the server-side program through the XmlHttpRequest object, for example, BuyServlet. java (servlet configuration defined by yourself). After receiving the request, the Servlet can change the content of a global variable you have defined, such as boolean buy = false, and change false to true. Then, the Administrator's background page also needs a corresponding javascript function. After the page is loaded, the Administrator continuously sends a request to another Servlet to read the variable (which can be twice ), once you find that the value you have defined is valid, you can access the client (this is the background administrator program) send a special value to indicate that the expected time has occurred (a bit like a listener ). Then the Administrator's javascript on the page determines this information and adds an alert () to the page (). Of course, remember to change the flag value back to the initial status after the notification is sent.

Of course, this method is not the best method. The problem is that there is no problem when a single user is used, but if it is a multi-user environment, several customers buy products at the same time, the Administrator page may have incorrect response times. You can set an ArrayList, insert a value for each change event, traverse the output, and clear one for each output until all values are cleared, which means no purchase is made at this time. Note that the lock mechanism is used for synchronization.

You need to score questions in the future. This is a respect for the respondent.

[AJAX] How to Use ajax technology to change the value in FreeTextBox without any changes

Reload FreeTextBox once in uppanel and assign a value.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.