2011-09-01 26 views

Trả lời

91

Bạn có thể sử dụng chức năng setTimeout():

// Your delay in milliseconds 
var delay = 1000; 
setTimeout(function(){ window.location = URL; }, delay); 
+2

lol gần như giống hệt nhau nhưng nhanh hơn 24 giây: P –

45

Bạn không thực sự cần jQuery cho việc này. Bạn có thể làm điều đó với javascript đơn giản bằng cách sử dụng phương pháp setTimeout:

// redirect to google after 5 seconds 
window.setTimeout(function() { 
    window.location.href = 'http://www.google.com'; 
}, 5000); 
12
$(document).ready(function() { 
    window.setInterval(function() { 
    var timeLeft = $("#timeLeft").html();         
     if(eval(timeLeft) == 0){ 
       window.location= ("http://www.technicalkeeda.com");     
     }else{    
      $("#timeLeft").html(eval(timeLeft)- eval(1)); 
     } 
    }, 1000); 
}); 
+2

Tôi sẽ không khuyến khích sử dụng 'eval()' vì nó có nhiều hậu quả nguy hiểm. Thay vào đó, tôi khuyên bạn nên sử dụng 'parseInt()'. – winhowes

+0

Cảm ơn bạn rất nhiều .. – Roni

4

chỉ cần sử dụng:

setTimeout("window.location.href='yoururl';",4000); 

..where các '4000' được m.second

6

Bạn có thể sử dụng

$(document).ready(function(){ 
     setTimeout(function() { 
     window.location.href = "http://test.example.com/;" 
     }, 5000); 
    }); 
2

Tôi đã thực hiện một si bản trình diễn đơn giản nhắc nhở số giây X và chuyển hướng để đặt url. Nếu bạn không muốn đợi đến khi kết thúc đếm, chỉ cần nhấp vào bộ đếm để chuyển hướng mà không mất thời gian. Truy cập đơn giản sẽ được đếm ngược trong khi thời gian rung ở giữa trang. Bạn có thể chạy nó onclick hoặc trong khi một số trang được tải.

LIVE DEMO ONLOAD

LIVE DEMO ONCLICK

Tôi cũng đã github repo cho việc này: https://github.com/GlupiJas/redirect-counter-plugin

JS mã ví dụ:

// FUNCTION CODE 
function gjCountAndRedirect(secounds, url) 
{ 

     $('#gj-counter-num').text(secounds); 

     $('#gj-counter-box').show(); 

    var interval = setInterval(function() 
    { 

     secounds = secounds - 1; 

     $('#gj-counter-num').text(secounds); 

     if(secounds == 0) 
     { 

      clearInterval(interval); 
      window.location = url; 
      $('#gj-counter-box').hide(); 

     } 

    }, 1000); 

    $('#gj-counter-box').click(function() //comment it out -if you dont want to allo count skipping 
    { 
     clearInterval(interval); 
     window.location = url; 

    }); 
} 

// USE EXAMPLE 
$(document).ready(function() { 
    //var 
    var gjCountAndRedirectStatus = false; //prevent from seting multiple Interval 

    //call 
    $('h1').click(function(){ 
     if(gjCountAndRedirectStatus == false) 
     { 
      gjCountAndRedirect(10, document.URL); 
      gjCountAndRedirectStatus = true; 
     } 
    }); 

}); 
1

Có, giải pháp là sử dụng setTimeout, như sau:

var delay = 10000; 
var url = "https://stackoverflow.com"; 
var timeoutID = setTimeout(function() { 
    window.location.href = url; 
}, delay); 

note rằng kết quả được lưu trữ vào timeoutID. Nếu vì bất cứ lý do bạn cần phải hủy bỏ lệnh, bạn chỉ cần gọi

clearTimeout(timeoutID); 
Các vấn đề liên quan