Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const codeDisplay = document.getElementById('generatedCode');
- const localStorageKey = 'lastGeneratedCode';
- // Función para generar un código aleatorio
- function generateCode(length = 6) {
- const characters = '0123456789';
- let code = '';
- for (let i = 0; i < length; i++) {
- code += characters.charAt(Math.floor(Math.random() * characters.length));
- }
- return code;
- }
- // Función para guardar el código en localStorage
- function saveCode(code) {
- localStorage.setItem(localStorageKey, code);
- }
- // Función para cargar el código desde localStorage al cargar la página
- function loadCode() {
- const savedCode = localStorage.getItem(localStorageKey);
- if (savedCode) {
- codeDisplay.innerText = savedCode;
- return savedCode;
- } else {
- const initialCode = generateCode();
- codeDisplay.innerText = initialCode;
- saveCode(initialCode);
- return initialCode;
- }
- }
- function generateNewCode() {
- const newCode = generateCode();
- document.getElementById('generatedCode').innerText = newCode;
- fetch('/.netlify/functions/server.js', { // URL de tu Netlify Function
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- },
- body: JSON.stringify({ codigo: newCode }),
- })
- .then(response => response.json())
- .then(data => {
- console.log('Respuesta del servidor (Netlify Function):', data);
- // Opcional: mostrar feedback al usuario
- })
- .catch(error => {
- console.error('Error al enviar el código a la función:', error);
- // Opcional: mostrar error al usuario
- });
- }
- loadCode();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement