2011-02-23 19 views
12
function timeClock() 
{ 
    setTimeout("timeClock()", 1000);   
    now = new Date(); 
    alert(now); 
    f_date = now.getDate()+" "+strMonth(now.getMonth())+" "+now.getFullYear()+"/"+timeFormat(now.getHours(), now.getMinutes()); 
    return f_date; 
} 

<span class="foo"><script type="text/javascript">document.write(timeClock());</script></span> 

अलर्ट (अब); मुझे हर सेकेंड का मूल्य देता है लेकिन यह एचटीएमएल में अपडेट नहीं होता है। मैं पृष्ठ को रीफ्रेश किए बिना HTML पर समय कैसे अपडेट कर सकता हूं?नियमित रूप से समय कैसे अपडेट करें?

उत्तर

27

अपने कोड में गलतियों की एक संख्या हैं। var के उपयोग के बिना आपकी परिवर्तनीय घोषणाओं के, आप उन्हें वैश्विक दायरे में रिसाव करते हैं।

इसके अलावा, document.write का उपयोग discouraged है।

यहाँ कैसे मैं यह कर देगा:

जावास्क्रिप्ट:

function updateClock() { 
    var now = new Date(), // current date 
     months = ['January', 'February', '...']; // you get the idea 
     time = now.getHours() + ':' + now.getMinutes(), // again, you get the idea 

     // a cleaner way than string concatenation 
     date = [now.getDate(), 
       months[now.getMonth()], 
       now.getFullYear()].join(' '); 

    // set the content of the element with the ID time to the formatted string 
    document.getElementById('time').innerHTML = [date, time].join('/'); 

    // call this function again in 1000ms 
    setTimeout(updateClock, 1000); 
} 
updateClock(); // initial call 

HTML:

<div id="time"> </div> 
+14

आईवो - अच्छा दुःख आदमी , मुझे यकीन है कि आप कठोर होने के बिना अपना समाधान दे सकते थे। कोई भी गलतियों के बिना है: "फिर" -> "से"? आ जाओ भाई। – d2burke

+0

आपको शायद प्रमुख शून्य के साथ 'now.getMinutes()' कॉल को पैड करना चाहिए। –

0
x = document.getElementsByTagName('SPAN').item(0); 
x.innerHTML = f_date; 

return बयान के बजाय इस कोड को ब्लॉक डाल कोशिश, मैं इसे परीक्षण नहीं लेकिन यह शायद काम करेंगे

+0

पहली बार documrnt .write अपरिभाषित मान दिखा रहा है। [23 फरवरी 2011/7.47 अपराह्न ISTundefined]। एक सेकंड के बाद यह मुझे सही मूल्य दिखाता है। – alex

0

TIMEAGO jQuery प्लगइन में कुछ आप में हुक कर सकते हैं हो सकता है, लेकिन मैं नहीं है ईमानदारी से कोशिश की ...

http://timeago.yarp.com/

-1
function timeClock() 
{ 
    setTimeout("timeClock()", 1000);   
    now = new Date(); 
    alert(now); 
    f_date = now.getDate()+" "+strMonth(now.getMonth())+" "+now.getFullYear()+"/"+timeFormat(now.getHours(), now.getMinutes()); 

    document.getElementById("foo").innerHTML = f_date; 

} 

<span id="foo"></span> 
0
$('span.foo').html(f_date); 

जगह यह आपके timeclock() समारोह अंदर

अपरीक्षित

function timeClock() 
    { 
     setTimeout("timeClock()", 1000);   
     now = new Date(); 
     alert(now); 
     f_date = now.getDate()+" "+strMonth(now.getMonth())+" "+now.getFullYear()+"/"+timeFormat(now.getHours(), now.getMinutes()); 
     $('span.foo').html(f_date); 

} 
0

मैं setTimeout बजाय setInterval का उपयोग करेंगे:

https://developer.mozilla.org/en/DOM/window.setInterval

+1

यह सलाह दी जाती है कि 'setInterval()' यहां एक नज़र डालें http://weblogs.asp.net/bleroy/archive/2009/05/14/setinterval-is-moderately-evil.aspx – Rafay

0

मुझे लगता है कि अपने setTmeout समारोह गलत वेरिएबल नहीं है, पहले एक एक फंक्शन एक स्ट्रिंग नहीं होना चाहिए, जो मुझे थोड़ा उलझन में डाल देता है। मूल रूप से आपको फ़ंक्शन चलाने पर स्पैन टैग पर लिखना होगा।

मैंने अपने मतलब का प्रदर्शन करने के लिए एक पहेली में एक jQuery संस्करण बनाया है। आपके strMonth फ़ंक्शन नहीं था लेकिन आपको विचार मिलता है। मैंने कंसोल.लॉग को अलर्ट भी बदल दिया लेकिन आप उस लाइन को हटा सकते हैं।

http://jsfiddle.net/5JWEV/

4

setInterval (अभिव्यक्ति, टाइमआउट);

फ़ंक्शन सेटटाइमआउट एक एकल टाइमआउट के लिए है, इसलिए setInterval का उपयोग करना एक और उपयुक्त विकल्प होगा। SetInterval Ivo के उत्तर की अतिरिक्त लाइनों के बिना नियमित रूप से चलाएगा।

मैं इस प्रकार इवो के जवाब पुनर्लेखन होगा:

जावास्क्रिप्ट:

function updateClock() { 
    // Ivo's content to create the date. 
    document.getElementById('time').innerHTML = [date, time].join('/') 
} 
setInterval(updateClock, 1000); 

खुद इसे आज़माकर देखें! https://jsfiddle.net/avotre/rtuna4x7/2/

0

straigt जावास्क्रिप्ट समय प्रारूप/अद्यतन

1: माह कनवर्टर समारोह 2 बनाने के लिए: समय समारोह 3 बनाने के लिए: अद्यतन समारोह 4 बनाएँ: बनाने के उत्पादन समारोह

// month converter from index/0-11 values 
function covertMonth(num){ 
    let months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']; 
    // look into index with num 0-11 
    let computedRes = months[num]; 
    return computedRes; 
} 

// time func 
function Time(){ 
    // important to get new instant of the Date referrance 
    let date = new Date(); 
    this.time = date.toLocaleTimeString(); 
    this.year = date.getUTCFullYear(); 
    this.day = date.getUTCDate(); 
    this.month = date.getUTCMonth(); 
    this.currentTime = date.toLocaleTimeString() + ' ' + covertMonth(this.month) + ' ' + this.day + ' ' + this.year; 
    return this.currentTime; 
} 


function timeOutPut(){ 
    let where = document.getElementById('some-id'); 
    where.textContent = Time(); // 1:21:39 AM Dec 17 2017 
} 

    // run every 5secs 
setInterval(timeOutPut, 5000); 
+0

कुछ विवरण जोड़ें – Billa

संबंधित मुद्दे