เด็กไอทีคลับ คลับซ่าฅนไอที

iTeach | ไอทีเพื่อการเรียนรู้ => Webmaster Recommend => ข้อความที่เริ่มโดย: เด็กไอทีคลับ ที่ 18, 02 2010, 01:26:11 PM



หัวข้อ: วิธีทำ radio online ให้โชว์เพลงที่กำลังเปิดแบบไม่ใช้ iframe
เริ่มหัวข้อโดย: เด็กไอทีคลับ ที่ 18, 02 2010, 01:26:11 PM
อ้างเนื้อหาที่ใช้
Jquery : setInterval or setTimeout
I was doing a live counter for some of my sites … so to make it “LIVE” I’ve used the Great Jquery library (if you dont know what’s jquery is … i will make a post soon about it and some JQUERY For dummies post)

What i have is simple :

- count.php // the counter basically it’s just a php file where i update the database
- index.php // the main page where i show the counter .

so this is what i wrote on the index.php
// The call to the count.php page , Using Post Method and sending 1 as a variable
function update() {
 
$.post("count.php", {count:1},
  function(data){
   $("#counter").html(data);});
}
 
// The code that call the update function each seconds
  $(document).ready(function() {
    setInterval("update()", 1000);
  })
So after writing this code , everything was working fine and the counter was live and the update worked .

I asked in few forums to see if there was any problem with my code since i’m new to jquery

here are some of the answers i got :

From a Sitepoint member hexburner
อ้างถึง
You could use an Ajax request with the if Modified header.

Or you could just use the load function.

The one second interval creates a huge amount of requests, which I wouldn’t recommend…

Nobody is really looking to the counter every second, so you can increase the interval without upsetting anybody.

What really concerns me is what happens if counter.php could not be loaded within the one second interval.

Imagine someones internet connection breaks down right after they visit your website.

The page counter.php could not be loaded, and after 30 seconds you’ll have 30 open connections…

So don’t use setInterval with ajax requests, but rather use setTimeout after the ajax request completed or returned an error.

I would recommend something like below, but you could use one of the other ajax functions of jQuery, whichever suits you best.

jQuery(document).ready(loadCounter);
 
function loadCounter () {
    $("#counter").load("count.php", {count:1}, function(responseText, textStatus, XMLHttpRequest){
        setTimeout(loadCounter, 5000);
    });
}
I tested his code and everything was fine and working .

The only concern i’m still having is the server issue , since the site is on a shared account … here is what another person from Google jquery group answered ”
อ้างถึง
If this is what you are after, and you are getting that data from a database, then a request must be sent every time to that database.

Seems to be pretty taxing, especially if traffic ramps up.

Joe

he’s right , in worse case what i will do is one of the followings :

- Make the call every 5 seconds instead of 1
- Make the call only on click call

Any other ideas for live counter ?

what about the live chat i think it’s the same idea behind …

---
จองไว้ก่อนเดี๋ยวมาเขียนต่อ


หัวข้อ: Re: วิธีทำ radio online ให้โชว์เพลงที่กำลังเปิดแบบไม่ใช้ iframe
เริ่มหัวข้อโดย: CoolPha ที่ 18, 02 2010, 03:43:51 PM
ว้าวสุดยอดเลยค่ะ ที่รักเราเก่งมาก

ดีด้วยค่ะอันนี้ ถึงมันจะช้าไปนิดก็เถอะนะ


หัวข้อ: Re: วิธีทำ radio online ให้โชว์เพลงที่กำลังเปิดแบบไม่ใช้ iframe
เริ่มหัวข้อโดย: CyberNoi ที่ 4, 03 2010, 05:27:19 PM
อยากบอกว่า  "มันเจ๋งมากๆๆๆ"
อิอิ ขอบคุณครับ


หัวข้อ: Re: วิธีทำ radio online ให้โชว์เพลงที่กำลังเปิดแบบไม่ใช้ iframe
เริ่มหัวข้อโดย: CoolPha ที่ 4, 03 2010, 08:11:44 PM
ป็อด สอบเสร็จ ยังอ่ะจ้ะ

มาบ่อย ๆ สิ


หัวข้อ: Re: วิธีทำ radio online ให้โชว์เพลงที่กำลังเปิดแบบไม่ใช้ iframe
เริ่มหัวข้อโดย: CyberNoi ที่ 5, 03 2010, 11:20:14 AM
สอบ 8-9 มีนา  ครับบบบ อิอิอิ
ช่วงนี้ยุ่งมากเลยครับ  หลายงาน เหอะๆๆๆ
แต่ยังไงจะมาบ่อยๆๆ แบ่งปันน้ำใจกันนน นะครับ อิอิ


หัวข้อ: Re: วิธีทำ radio online ให้โชว์เพลงที่กำลังเปิดแบบไม่ใช้ iframe
เริ่มหัวข้อโดย: paemam ที่ 29, 03 2010, 05:49:34 PM
จะลองเอาไปทำดูดีกว่า


หัวข้อ: Re: วิธีทำ radio online ให้โชว์เพลงที่กำลังเปิดแบบไม่ใช้ iframe
เริ่มหัวข้อโดย: CoolPha ที่ 30, 03 2010, 08:11:19 PM
อืม ลองทำดูค่ะ

บวก หนึ่งให้แล้วนะค่ะ

ยินดีต้อนรับค่ะ