2014-10-22 10 views
7

में टाइपिंग शुरू/बंद कर रहा है, जब मैं अपनी समस्या के लिए समाधान खोज रहा था और मेरा मुद्दा था "मैं जानना चाहता हूं कि उपयोगकर्ता टाइप कर रहा है और जब वह टाइप करना बंद कर देता है ताकि मैं स्थिति अपडेट कर सकूं।"पता लगाएं कि उपयोगकर्ता jquery

मैंने एक नमूना बनाया है। यह आपके लिए काम करेगा।

var typingTimer; 
var doneTypingInterval = 10; 
var finaldoneTypingInterval = 500; 

var oldData = $("p.content").html(); 
$('#tyingBox').keydown(function() { 
    clearTimeout(typingTimer); 
    if ($('#tyingBox').val) { 
    typingTimer = setTimeout(function() { 
     $("p.content").html('Typing...'); 
    }, doneTypingInterval); 
    } 
}); 

$('#tyingBox').keyup(function() { 
    clearTimeout(typingTimer); 
    typingTimer = setTimeout(function() { 
    $("p.content").html(oldData); 
    }, finaldoneTypingInterval); 
}); 



<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 



<textarea id="tyingBox" tabindex="1" placeholder="Enter Message"></textarea> 
<p class="content">Text will be replace here and after Stop typing it will get back</p> 

View on Fiddle : http://jsfiddle.net/utbh575s/

+0

SO में आपका स्वागत है! क्या पूछते हैं? – georg

+0

आपका कोड काम कर रहा है .. इस प्रश्न का उद्देश्य क्या है? – rinuthomaz

+3

हाय, हाँ यह काम कर रहा है कि मैंने इसे क्यों साझा किया है। यह दूसरों के लिए भी उपयोगी होगा। –

उत्तर

4

हो सकता है कि क्या आप चाहते हैं debounce कार्यक्षमता है।

असल में यह उस दर को सीमित करता है जिस पर एक फ़ंक्शन आग लग सकता है। इसलिए यह लिखने की प्रक्रिया को रोकने वाले उपयोगकर्ता की घटना को फायर करने से पहले कुछ एमएस प्रतीक्षा करता है।

चेक इस स्निपेट

// Returns a function, that, as long as it continues to be invoked, will not 
 
// be triggered. The function will be called after it stops being called for 
 
// N milliseconds. If `immediate` is passed, trigger the function on the 
 
// leading edge, instead of the trailing. 
 
function debounce(func, wait, immediate) { 
 
\t var timeout; 
 
\t return function() { 
 
\t \t var context = this, args = arguments; 
 
\t \t var later = function() { 
 
\t \t \t timeout = null; 
 
\t \t \t if (!immediate) func.apply(context, args); 
 
\t \t }; 
 
\t \t var callNow = immediate && !timeout; 
 
\t \t clearTimeout(timeout); 
 
\t \t timeout = setTimeout(later, wait); 
 
\t \t if (callNow) func.apply(context, args); 
 
\t }; 
 
}; 
 

 
// This will apply the debounce effect on the keyup event 
 
// And it only fires 500ms or half a second after the user stopped typing 
 
$('#testInput').on('keyup', debounce(function() { 
 
    alert('typing occurred'); 
 
    $('.content').text($(this).val()); 
 
}, 500));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="text" id="testInput" /> 
 

 
<p class="content"></p>

मूल रूप से अब यह आप पर निर्भर है। एमएस में अपना समय निर्धारित करें और आप जाने के लिए अच्छे हैं।

+1

वास्तव में उपयोगी। धन्यवाद – Gateway

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