From e7144692c59f0700e1d9697269923c0e415b1a76 Mon Sep 17 00:00:00 2001
From: nathanlao
-const [rawHTMLContent, setRawHTMLContent] = useState('');
+const [rawHTMLContent, setRawHTMLContent] = useState('');
@@ -42,7 +42,7 @@
method: 'get',
responseType: 'text'
});
- return response;
+ return response;
} catch (error) {
console.error('Error fetching the HTML content:', error);
}
diff --git a/src/posts/How Do I Use setInterval() For Repeating A Content.html b/src/posts/How Do I Use setInterval() For Repeating A Content.html
deleted file mode 100644
index f2e006b..0000000
--- a/src/posts/How Do I Use setInterval() For Repeating A Content.html
+++ /dev/null
@@ -1,3 +0,0 @@
- Coming soon.. Timing events can be useful when you want to execute the code at specified time intervals. setInterval() is one of them that allows us to repeatly run a function. I want to use setInterval() to repeatly render a string content in React: You may wanna create a Typewriter effect by modifying the logic a bit, but it could be tricky here with the React state updates here, the second character is skipped because index is incremented twice before the set state inner function has run once. Consider using a ref to store your string value (updating a reference will not trigger a component re-rendering): This is it!
+
+const [dots, setDots] = useState('');
+
+useEffect(() => {
+ let dotCount = 0;
+ const interval = setInterval(() => {
+ dotCount = (dotCount + 1) % 4; // Reset the count in every 3 strings
+ let dots = '';
+ for (let i = 0; i < dotCount; i++) {
+ dots += '.';
+ }
+ setDots(dots);
+ }, 600);
+ return () => clearInterval(interval);
+}, []);
+
+
+
+const [text, setText] = useState('');
+const fullText = Hello world!;
+const index = useRef(0);
+const displayTextRef = useRef('');
+
+useEffect(() => {
+ const interval = setInterval(() => {
+ if (index.current < fullText.length) {
+ displayTextRef.current += fullText.charAt(index.current);
+ setText(() => displayTextRef.current);
+ index.current += 1;
+ } else {
+ clearInterval(interval);
+ }
+ }, 500);
+ return () => {
+ setText('');
+ clearInterval(interval);
+ };
+}, []);
+
+