Devdit
 

PHP ตั้งเวลาเปลี่ยนหน้า ด้วย jQuery

656

PHP ตั้งเวลาเปลี่ยนหน้า ด้วย jQuery บทความนี้สอนเขียนโปรแกรมภาษา PHP ร่วมกับ jQuery Framework เพื่อตั้งเวลาเปลี่ยนหน้า โดยใช้คำสั่ง setInterval ร่วมกับ window.location สามารถเขียนโปรแกรมได้ดังนี้

 

ตัวอย่าง PHP ตั้งเวลาเปลี่ยนหน้า ด้วย jQuery

<center>
<div>กรุณารอนับถอยหลัง 5 วินาที เพื่อเปลี่ยนหน้า</div>
<div id="counter" style="color: red; font-size: 2rem; margin-top: 0.5rem;"></div>
</center>
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
<script>
    $(document).ready(function(){
        let counter = 0;
        setInterval(() => {
            counter++;
            $("#counter").html( counter );
            if( parseInt(counter) == 5 ) {
                window.location = "https://www.devdit.com/";
            }
        }, 1000);
    });
</script>

คำอธิบาย

PHP ตั้งเวลาเปลี่ยนหน้า จากตัวอย่างแสดงเวลานับถอยหลังที่ div id ="counter" โดยใช้คำสั่ง setInterval ในการนับทุกๆ 1000 มิลลิวินาที = 1 วินาที กรณีถ้านับถึง 5 วินาทีให้เปลี่ยนหน้าด้วยคำสั่ง window.location

แก้ไข 1 ปีที่แล้ว
ชอบ
ลิ้งก์
แชร์
Devdit มีช่อง YouTube แล้ว
เราสร้างวิดีโอเกี่ยวกับเทคโนโลยี ทำตามง่ายๆ