Very slow javascript in safari mobile

Asked

Viewed 117 times

1

In my application has a siples javascript to increment a field, the problem that in safari gets ultra slow almost does not work, while in Chrome works perfectly. There is something to do to run normal on safari?

js:

$("#aumentaDesconto").click(function () {
var input = $("#txtDesconto")[0];
var desconto = parseInt(input.value, 10) + 1;
input.value = desconto;

});
  • 1

    Post also the function calcularValor.

  • I’m actually going to remove it because Ian has nothing bird with my problem.

  • Slow to increase field values.

  • I find it hard to blame being in this code, but anyway try var desconto = +input.value + 1;

  • I think it’s the safari’s fault, I saw several reports of slow safari and javascript, but I need the same running in safari.I will test your code

  • 1

    Utilize console.count('teste') inside the function and see on your console how many times the function has been called. Maybe you left this code inside a loop and the event was linked several times.

Show 1 more comment

2 answers

1

The way you did, the code performs a search in your HTML for the id element txtDesconto every time #aumentaDesconto is clicked.

One way to optimize is to cache the elements used repeatedly, because searching in Domtree is quite costly for the browser.

Try it like this:

var input_desconto = document.getElementById("txtDesconto"),
    aumenta_desconto = document.getElementById("aumentaDesconto");

aumentaDesconto.addEventListener("click", function () {
    input.value = parseInt(input_desconto.value, 10) + 1;
}, false);

I made a Jsfiddle.

  • Remains slow, tested on ipad and iphone, safari really does not roll...

  • 1

    Are you sure this is the script you’re slowing down? Have you tried taking it out and see how the page behaves without the script?

1

The only thing I see can optimize here is to remove jQuery and do it with pure Javascript. It would look like this:

document.getElementById("aumentaDesconto").addEventListener('click', function () {
    var input = document.getElementById("txtDesconto");
    var desconto = parseInt(input.value, 10) + 1;
    input.value = desconto;
});

If it doesn’t solve the problem I suggest you put a jsFiddle that reproduces the problem so we can analyze it.

  • I did it that way and the increment didn’t work.

  • @Warlock had left one # plus, fixed now. Put this script at the end of body and test.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.